fife Posted February 5, 2013 Share Posted February 5, 2013 I'm having trouble comparing two arrays. Ive never worked with arrays like this till today so bare with me. Ill explain as best I can with my code. So I have one array Im pulling from the database. which I get as follows //now get all the tags mysql_select_db($db, $db); $getq = mysql_query("SELECT * FROM blogtags",$db); //create the tags array $arr1 = array(); do{ $arr1[] = $row['tagname']; }while($row = mysql_fetch_assoc($getq)); If I print the outcome of this $arr1 array(3) { [0]=> NULL [1]=> string(4) "cars" [2]=> string(4) "vans" } cars and vans are the only 2 entries in the database Next I have a form field called $_POST['hidden-tags']; which stores data as ....... tanks, bikes, trains here is the code I have to turn that into an array $arr2[] = explode(",",$_POST['hidden-tags']); if I echo that out I get array(1) { [0]=> array(4) { [0]=> string(4) "cars" [1]=> string(5) "bikes" [2]=> string(6) "trains" [3]=> string(5) "tanks" } } which are entries into the form (cars, bikes, trains, tanks) Now Im trying to only show items that are not in the first array with array_diff but for some reason it doesnt work correctly. First I can see in the second array does not look like the first and I dont understand why but im sure thats coursing problems!!!!! secondly If I run the code //show only items in the second array that are not in the first $tmp = array_diff($arr2, $arr1); var_dump($tmp); then this pops out array(1) { [0]=> array(4) { [0]=> string(4) "cars" [1]=> string(5) "bikes" [2]=> string(6) "trains" [3]=> string(5) "tanks" } } surely this should just show bikes, trains, tanks and not the cars as it appears in $arr1 Quote Link to comment https://forums.phpfreaks.com/topic/274070-drowning-in-comparison-of-2-arrays/ Share on other sites More sharing options...
Jessica Posted February 5, 2013 Share Posted February 5, 2013 $arr2[] = explode(",",$_POST['hidden-tags']); Remove the [] and your arrays will have the same number of dimensions. Quote Link to comment https://forums.phpfreaks.com/topic/274070-drowning-in-comparison-of-2-arrays/#findComment-1410314 Share on other sites More sharing options...
fife Posted February 5, 2013 Author Share Posted February 5, 2013 omg that worked!!!!!!!!!!!!!! thank you very much thats been getting me down all day. Love this forum. you guys always rock sox!! Quote Link to comment https://forums.phpfreaks.com/topic/274070-drowning-in-comparison-of-2-arrays/#findComment-1410317 Share on other sites More sharing options...
Psycho Posted February 5, 2013 Share Posted February 5, 2013 You should NOT use a do . . . while() loop when processing your database results. That is why you get a NULL value at the beginning of the array. do{ $arr1[] = $row['tagname']; }while($row = mysql_fetch_assoc($getq)); On the first iteration of that loop $row is not defined. Use a while() loop instead while($row = mysql_fetch_assoc($getq)) { $arr1[] = $row['tagname']; } Quote Link to comment https://forums.phpfreaks.com/topic/274070-drowning-in-comparison-of-2-arrays/#findComment-1410330 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.