torvald_helmer Posted April 13, 2007 Share Posted April 13, 2007 I have a mulitarray. Each entry in the array consist of another array. Each of these consist of words, one word per index. array [ 0 = [arr1 = yellow, white, red] 1 = [arr2 = blue, red, green] 2 = [arr3 = violet, black, orange] ] Can I use in_array to see if a special word exists in all of these array? I mean does in_array work for multiarrays, will the function check all of the subarrays? And is there a way I can get the count of of how many array's the special word exist? E.g. take the word 'red' would have the count 2, because is exists in arr1 and arr2. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted April 13, 2007 Share Posted April 13, 2007 Check the PHP manual. See the user comments; I believe this has been addressed to multidimensional arrays. If you need a count, you may need to modify the example. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 13, 2007 Share Posted April 13, 2007 use in_array() on each of the sub_arrays <?php $a = array( array ('yellow', 'white', 'red'), array ('blue', 'red', 'green'), array ('violet', 'black', 'orange') ); $needle = 'red'; $count = 0; foreach ($a as $subarray) { if (in_array($needle, $subarray)) ++$count; } echo $count; ?> Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 13, 2007 Share Posted April 13, 2007 here's how you do it: <?php function find($needle, $haystack){ foreach($haystack as $h_key => $h_val){ if(is_array($h_val)){ find($needle, $h_val); } foreach($needle as $n_key => $n_val){ if($n_val == $h_val){ echo "Found: {$n_val} in the array.<br />\n"; } } } } /*all matches you are looking for*/ $needle = array('this', 'is', 'what', 'i am', 'looking for'); /*all the values you're searching in*/ $haystack = array( array('is', 'this'), array('what', 'you', 'were'), array('looking for') ); find($needle, $haystack); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted April 13, 2007 Share Posted April 13, 2007 An excellent solution looking for a problem But at what point does it tell torvald that "red" was found in 2 arrays? Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 13, 2007 Share Posted April 13, 2007 you're right barand, my function doesn't do that. but this one will =) <?php function find($needle, $haystack){ $found = array(); foreach($haystack as $h_key => $h_val){ if(is_array($h_val)){ find($needle, $h_val); } foreach($needle as $n_key => $n_val){ if(in_array($n_val, $h_val)){ if(!isset($found[$h_val])){ $found[$h_val] = array($n_val => 1); }elseif(isset($found[$h_val][$n_val]){ $found[$h_val][$n_val]++; } } } } foreach($found as $key => $val){ foreach($val as $match => $amount){ echo "{$match} was found {$amount} time(s).<br />\n"; } echo "in the {$key} index of the parent array.<br /><br />\n"; } } /*all matches you are looking for*/ $needle = array('this', 'is', 'what', 'i am', 'looking for'); /*all the values you're searching in*/ $haystack = array( array('is', 'this'), array('what', 'you', 'were'), array('looking for') ); find($needle, $haystack); ?> whaddyoo think? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 13, 2007 Share Posted April 13, 2007 Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\Inetpub\wwwroot\test\noname5.php on line 8 Warning: Illegal offset type in isset or empty in C:\Inetpub\wwwroot\test\noname5.php on line 9 Ran this /*all matches you are looking for*/ $needle = array('this', 'is', 'what', 'i am', 'looking for'); /*all the values you're searching in*/ $haystack = array( array('is', 'this'), array('what', 'you' => array('he is', 'i am'), 'were'=> array('i am', 'i was')), array('looking for') ); find($needle, $haystack); Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 13, 2007 Share Posted April 13, 2007 i apologize, but the code i posted earlier is not correct. i hardly ever test my code before posting. my code examples are generally only used for conceptual analysis... this, however, should work: <?php function find($needle, $haystack){ $found = array(); foreach($haystack as $h_key => $h_val){ foreach($needle as $n_key => $n_val){ if(in_array($n_val, $h_val)){ if(!array_key_exists($h_key, $found)){ $found[$h_key] = array($n_val => 1); }elseif(array_key_exists($n_val, $found[$h_key])){ $found[$h_key][$n_val]++; } } } } foreach($found as $key => $val){ foreach($val as $match => $amount){ echo "'{$match}' was found {$amount} time(s).<br />\n"; } echo "in the [{$key}] index of the parent array.<br /><br />\n"; } } /*all matches you are looking for*/ $needle = array('this', 'is', 'what', 'i am', 'looking for'); /*all the values you're searching in*/ $haystack = array( array('is', 'this'), array('what', 'you', 'were'), array('looking for') ); find($needle, $haystack); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted April 13, 2007 Share Posted April 13, 2007 So if we use Torvald's original data /*all matches you are looking for*/ $needle = array('red'); /*all the values you're searching in*/ $haystack = array( array ('yellow', 'white', 'red'), array ('blue', 'red', 'green'), array ('violet', 'black', 'orange') ); find($needle, $haystack); it gives red was found 1 time(s). in the [0] index of the parent array. red was found 1 time(s). in the [1] index of the parent array. So if it occurred in 1,000 arrays, he has to count your output of how many times it occurred once in 1000 arrays :/ Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 13, 2007 Share Posted April 13, 2007 And is there a way I can get the count of of how many array's the special word exist? E.g. take the word 'red' would have the count 2, because is exists in arr1 and arr2. isn't that exactly what he wanted? it would have been much easier to just count how many times a match occurs and bear no reference to where the match was discovered. i wrote it that way because i thought that's what he meant in the quote above. if that is the case, is there anything you'd change about my script? i can easily modify it to only list how many times a match was found, but i'd like to know if you have any advice for the code applied to what i think the objective was. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 13, 2007 Share Posted April 13, 2007 All he asked for was a count of the arrays it appears in. So http://www.phpfreaks.com/forums/index.php/topic,135855.msg573592.html#msg573592 No need to build a theme park when they only want a garden swing. Your original code was recursive, which was a nice touch though. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 13, 2007 Share Posted April 13, 2007 Your original code was recursive, which was a nice touch though. thanks. i thought so too, but it didn't work properly =\. it kept throwing this error: Warning: in_array(): Wrong datatype for second argument on line 13 i couldn't figure out why... it checks to see if it's an array, then plugs it into the function... but as far as i know, it's telling me that it's not an array... i couldn't figure it out, so i took out the condition... can you see where i'm going wrong? if anything for future reference. thanks. <?php function find($needle, $haystack){ $found = array(); foreach($haystack as $h_key => $h_val){ if(is_array($h_val)){ find($needle, $h_val); } foreach($needle as $n_key => $n_val){ if(in_array($n_val, $h_val)){ //line 13 if(!array_key_exists($h_key, $found)){ $found[$h_key] = array($n_val => 1); }elseif(array_key_exists($n_val, $found[$h_key])){ $found[$h_key][$n_val]++; } } } } foreach($found as $key => $val){ foreach($val as $match => $amount){ echo "'{$match}' was found {$amount} time(s).<br />\n"; } echo "in the [{$key}] index of the parent array.<br /><br />\n"; } } /*all matches you are looking for*/ $needle = array('this', 'is', 'what', 'i am', 'looking for'); /*all the places you're searching in*/ $haystack = array( array('is', 'this'), array('what', 'you', 'were'), array('looking for') ); find($needle, $haystack); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted April 13, 2007 Share Posted April 13, 2007 When it first hits this line if(in_array($n_val, $h_val)){ $n_val contains 'this' and $h_val contains 'is' Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 13, 2007 Share Posted April 13, 2007 ah i see! i can't believe i couldn't work out the logic before. because the recursive feature will 'recurse' until $h_val is a string, not an array... so... i'm stumpted. i'm not sure how i'd get the functionality i'm looking for. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 13, 2007 Share Posted April 13, 2007 try <?php function find ($needle, $haystack, &$count) { foreach ($haystack as $item) { if (is_array($item)) find ($needle, $item, $count); elseif ($item == $needle) ++$count; } } /*all matches you are looking for*/ $needle = array('this', 'is', 'what', 'i am', 'looking for'); /*all the places you're searching in*/ $haystack = array( array('is', 'this'), array('what', 'you' => array('i am', 'we were'), 'were'=>array('he was', 'i am') ), array('looking for') ); foreach ($needle as $word) { $count = 0; find($word, $haystack, $count); // just have find() find one at a time echo "$word : $count<br>"; } ?> Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 13, 2007 Share Posted April 13, 2007 dammit Barand, why do you gotta be so good! i've got a long way to go... Quote Link to comment 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.