c_pattle Posted January 3, 2012 Share Posted January 3, 2012 I have a section in one of my scripts that checks for a value in array and the value doesn't exist in the array then it add it. This works fine for single array but not for mulit-dimensional arrays. Has anyone had a similar problem and knows a good solution? if(in_array($words[$i], $searchWords)){ //Do nothing }else{ $searchWords[] = $words[$i]; } Quote Link to comment https://forums.phpfreaks.com/topic/254274-in_array-not-working/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2012 Share Posted January 3, 2012 Have you checked if $words[$i] has the value you expect? Do you have any white-space as part of the data in the $words array that would prevent a comparison from working? Are the letter cases the same? If $words is already an array, you can just use array_diff to find the words that are not already in the second array, then to add them to the second array, just use array_merge. There's no need to loop. Quote Link to comment https://forums.phpfreaks.com/topic/254274-in_array-not-working/#findComment-1303722 Share on other sites More sharing options...
ManiacDan Posted January 3, 2012 Share Posted January 3, 2012 in_array is not recursive, you have to write your own recursive version. Show us what $searchWords looks like. Quote Link to comment https://forums.phpfreaks.com/topic/254274-in_array-not-working/#findComment-1303756 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.