/    Sign up×
Community /Pin to ProfileBookmark

how to compare 2 arrays ?

Hello

I am having problems comparing 2 arrays .

I have $domains[] which contains 3 values

123.com
456.com
[b]789.com[/b]

and $data[] which contains 3 values arriving from a data file

566.com
[b]789.com[/b]
555.com

I wish to compare the array $domains[] with array $data[] (each value of array $domains[] with
each value of $data[] ) . I wish to echo “OK” if two arrays are the same (==),
I wish to echo “FAIL” if the arrays are not the same.
So the result of the script should be

$domains[] compare result
123.com FAIL
456.com FAIL
[b]789.com[/b] OK

I am trying in this way

[code]


$data_file = ‘/data.txt’;
if (!empty($HTTP_POST_VARS[‘is_sent’])) process_form();
$data = file($data_file);
$count_data = count($data);

$countx=3;
for ($x = 0; $x < $countx; $x++)
{

echo”$domains[$x]”;
for ($xc = 0; $xc <= $count_data-1 ; $xc++)
{
$data[$xr] = trim($data[$xr]);

if ( $domains[$x] == $data[$xc] ) {
echo “OK<br>”;
# stop $xc for cycle because condition verified
$xr=$count_data;
}
else
{
echo “FAIL;
}
}
}

[/code]

but it doesn’t work ..

Is there a better way to do the work ?

Thank you!

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@bathurst_guyNov 21.2006 — [code=php]foreach($domains as $key => $value){
$ok = 0;
foreach($data as $hole => $other){
if($value == $other){
echo "$value - OK<br>";
$ok = 1;
}
}
if($ok == 0)
echo "$value - FALSE<br>";
}[/code]
Copy linkTweet thisAlerts:
@grazianoauthorNov 21.2006 — Thank you

Your code is right , unluckly it fails because

I noticed that the array which arrives from $data contains some space in their values

argh...
Copy linkTweet thisAlerts:
@bokehNov 21.2006 — the array which arrives from $data contains some space[/QUOTE][code=php]$data = array_map('trim', $data);[/code]
Copy linkTweet thisAlerts:
@grazianoauthorNov 21.2006 — Thank you , in fact this was the solution

########################################

$data = array_map('trim', $data );

########################################

I found it on php.net (on a note) and now you reported exactly the same

solution . I needed 4 hours to find this solution , and I have still to

understand why "$data[$x]=str_replace(' ','',$data[$x]); " was not accepted (it didn't remove spaces from array) .

THANK YOU
Copy linkTweet thisAlerts:
@bokehNov 21.2006 — I have still to

understand why "$data[$x]=str_replace(' ','',$data[$x]); " was not accepted (it didn't remove spaces from array) .[/QUOTE]
Probably because what you are looking for is not a space. Trim removes other characters (ASCII 0, 9, 10, 11, 13 and 32).
×

Success!

Help @graziano spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...