LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2006, 01:07 AM   #1
graziano1968
Member
 
Registered: Sep 2004
Posts: 223

Rep: Reputation: 30
[php] how to compare 2 arrays ?


Hello

I am having problems comparing 2 arrays .

I have $domains[] which contains 3 values

123.com
456.com
789.com

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

566.com
789.com
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
789.com 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;
}
}
}

but it doesn't work ..

Is there a better way to do the work ?

Thank you!
 
Old 11-21-2006, 06:06 AM   #2
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Your algorithm looks fine, but it appears that a variable you don't want has crept in to the code. Your inside loop counter is $xc, but then you have $xr in there out of nowhere. Sorry if I missed something obvious, but I'm not seeing where that gets its value.

If that is the issue, the result will be that your attempt to break out of the inner for loop will fail. Is that what you are getting?
 
Old 11-21-2006, 07:30 AM   #3
graziano1968
Member
 
Registered: Sep 2004
Posts: 223

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by zaichik
Your algorithm looks fine, but it appears that a variable you don't want has crept in to the code. Your inside loop counter is $xc, but then you have $xr in there out of nowhere. Sorry if I missed something obvious, but I'm not seeing where that gets its value.

If that is the issue, the result will be that your attempt to break out of the inner for loop will fail. Is that what you are getting?
My code was not working because I had some space on the array (over the typo error you reported).
This was the solution

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

I found it on php.net (on a note) . 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 however
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Classes and Arrays joelhop Programming 2 07-24-2006 05:29 PM
php classes, arrays towlie Programming 10 06-07-2005 11:10 AM
php arrays and mysql simpleguy Programming 2 07-08-2004 06:28 AM
PHP Calculate, Compare.. Pts Gerardoj Programming 8 05-26-2004 12:54 AM
php date compare omarswan Programming 2 10-02-2002 04:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration