Jump to content

in_array function with multiarray's


torvald_helmer

Recommended Posts

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.

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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? ;)

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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 :/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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>"; 
        }
            
?>

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.