Jump to content

Comparing values within two different arrays and pulling my teeth out


soma56

Recommended Posts

This has kept me busy for a couple of hours. I have two arrays that I would like to compare against each other to see matches and otherwise. Simple enough, right?

 

		foreach ($array1 as $value){
			if (in_array($value, $array2)) {
			 echo $value."-FOUND<br />";
				} else {
			 echo $value."-NOT!<br />";
				}
		}

 

Using print_r I can CLEARLY see matches:

 

array1 = Array ( [0] => CHARLES [1] => TOM [2] => DICK [3] => HARRY)

array2 = Array ( [0] => HARRY [1] => DICK [2] => TOM [3] => CHARLES)


 

The only difference I can see is the keys - but why should that matter for what I'm trying to do?

 

Both arrays were converted to uppercase. I have done variations of search_array, array_intersect and array_unique - but absolutely nothing is working. 3 hours later and I'm no further then I started off. WTF?

Link to comment
Share on other sites

You're making this much more difficult on yourself than you need to. Take a look in the manual and you will find many existing functions for what you want.

 

$both  = array_intersect($array1, $array2);
$diff1 = array_diff ($array1, $array2);
$diff2 = array_diff ($array2, $array1);

echo "<pre>";
echo "These values exist in both array1 and array2:\n";
print_r($both);

echo "\n\nThese values exist in array1 but not in array2:\n";
print_r($diff1);

echo "\n\nThese values exist in array2 but not in array1:\n";
print_r($diff2);

Link to comment
Share on other sites

mjdamato, thanks for the great reply. None of it works. Here's why - although the values 'LOOK' identical they are not. I tried a different approach which made me discover this. Assuming the first array is '$name' and the second is in a table...

 

<?PHP 
		foreach ($name as $v){
		$query= "SELECT your_name FROM names WHERE your_name = '$v'";

		$result = mysql_query($query);

		$row = mysql_fetch_row($result);

		$name = $row[0];
		echo $name."<br />";

		}
?>

 

This doesn't work - but if change one thing - the $v variable to a name that is ALREADY in the array it does work, like so:

 

<?PHP 
		foreach ($name as $v){
		$query= "SELECT your_name FROM names WHERE your_name = 'Charles'";

		$result = mysql_query($query);

		$row = mysql_fetch_row($result);

		$name = $row[0];
		echo $name."<br />";

		}
?>

 

My first conclusion was white space. So I added this prior to connecting to the table:

 

$v = trim($v);

 

So it's something to do with the first array that I have. What I'm finding as a total mindf#$@ is that, as with the example above, if I simply paste the first array value (any value) in the query it works.

Link to comment
Share on other sites

what do you see if you use var_dump on the first array? this might be a way to detect the (sometimes difficult-to-spot) differences in the formatting.

 

EDIT: i've also just noticed that you reassign $name the value obtained in the query... is that intentional? it will kill the loop and/or trigger an error...

Link to comment
Share on other sites

mjdamato, thanks for the great reply. None of it works.

 

What?! You asked about finding like values in two different arrays. Now you are posting code that is doing database queries. Try explaining what you are trying to achieve instead of only giving half the info because you *think* you know what you are doing. Besides, you should NEVER be running queries in a loop like that.

Link to comment
Share on other sites

I see one messed up looking array....

 

array(52) { [0]=> string(4) "TEST" [1]=> string(14) "Nick Boynton " [2]=> string(16) "Jason Garrison " [3]=> string(15) "Bruno Gervais " [4]=> string(17) "Barrett Jackman " [5]=> string(15) "Chris Pronger " [6]=> string(15) "Anton Babchuk " [7]=> string(13) "Johny Oduya " [8]=> string(15) "Kyle Brodziak " [9]=> string(13) "Ville Leino " [10]=> string(17) "Mike Santorelli " [11]=> string(14) "Ryan Shannon " [12]=> string(14) "Raffi Torres " [13]=> string(13) "Kyle Turris " [14]=> string(11) "Joel Ward " [15]=> string(15) "Casey Wellman " [16]=> string(18) "Erik Christensen " [17]=> string(15) "Patrick Sharp " [18]=> string(19) "Fabian Brunnstrom " [19]=> string(18) "Gregory Campbell " [20]=> string(18) "Sergei Kostitsyn " [21]=> string(14) "Brooks Laich " [22]=> string(14) "Rick Peverly " [23]=> string(14) "Evander Kane " [24]=> string(19) "Goalies-b Chicago " [25]=> string(24) "Goalies-a Ny islanders " [26]=> string(24) "Goalies-a Philadelphia " [27]=> string(13) "Wade Redden " [28]=> string(17) "Viktor Tikhonov " [29]=> string(16) "Johan Fransson " [30]=> string(15) "Cody Goloubef " [31]=> string(17) "Brandon Gormley " [32]=> string(12) "Alex Grant " [33]=> string(13) "Paul Postma " [34]=> string(19) "Kevin Shattenkirk " [35]=> string(14) "Beau Bennett " [36]=> string(19) "Francois Bouchard " [37]=> string(22) "Alexander Burmistrov " [38]=> string(14) "Bobby Butler " [39]=> string(14) "Aaron Gagnon " [40]=> string(16) "Quinton Howden " [41]=> string(16) "Calle Jarnkrok " [42]=> string(16) "Kiril Kabanov " [43]=> string(15) "Louis Leblanc " [44]=> string(15) "Brad Marchand " [45]=> string(14) "Tyler Seguin " [46]=> string(15) "Riley Sheahan " [47]=> string(12) "Ilya Zubov " [48]=> string(23) "Mats Zuccarello-aasen " [49]=> string(14) "T.j. Hensick " [50]=> string(20) "Branko Radivojevic " [51]=> string(13) "Asgsg Uiowern" } 

 

The array (first array) was created from two variables (first and last name) coming in from a csv file, however, looking at it now I can see that it's not coming back the way I expected it to. I was simply using 'print_r' to see the data - which looks correct.

 

I suspect the problem lies with how the first array was created in the first place:

 


///CSV names coming in...
$name = $data[2]." ".$data[3];

array_push($first_array, $name);

 

What?! You asked about finding like values in two different arrays. Now you are posting code that is doing database queries. Try explaining what you are trying to achieve instead of only giving half the info because you *think* you know what you are doing. Besides, you should NEVER be running queries in a loop like that.

 

No need to be rude. I'm just entertaining different ways to get what I want.

Link to comment
Share on other sites

that data looks correct, except that every value between the first and last has a trailing space. trim *should* take care of that. once you run trim() against all values in the array (array_map comes in handy here), you can simply concatenate all the values into one comma-delimited string and put that into a SINGLE query:

 

$trimmed_version = array_map('trim', $name);
$single_list = implode(',', $trimmed_version);
$query = "SELECT stuff FROM table WHERE name_column IN ($single_list)";

 

i would suspect that the actual error occurs when, at the end of the loop with the query in it, you assign:

 

$name = $row[0];
echo $name."<br />";

 

that would reset the $name variable and kill the foreach() loop.

Link to comment
Share on other sites

Brilliant. Thank you akitchin! I was using the trim() function in the foreach statement previous to your post - everyone looked the same - the only logical conclusion was white space. As a result of reading your last post it occurred to me that maybe I should trim both of the values prior to placing them into an array. I decided to trim both the first and last name as I was receiving while the script was receiving the data from the CSV file.

 

<?PHP
        $first_name = trim($data[2]);
$last_name = trim($data[3]);
$name = $first_name." ".$last_name;
$first_array[] = $name;
?>

 

Voila! The I can relax again...

 

Thanks again, and for everyone's insight.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.