Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
<?php
static $cnt=0;
$name='victor';
$coll = array(
    'dep1'=>array(
        'fy'=>array('john','johnny','victor'),
        'sy'=>array('david','arthur'),
        'ty'=>array('sam','joe','victor')
    ),
        'dep2'=>array(
        'fy'=>array('natalie','linda','molly'),
        'sy'=>array('katie','helen','sam','ravi','vipul'),
        'ty'=>array('sharon','julia','maddy')
    )
    );

    function array_find($name,$arr)
    {
        global $cnt;
    if(!(is_array($arr)))
        return false;

    foreach($arr as $val)
    {
        if(is_array($val))
        array_find($name,$val);
        else
        {
        $val=strtolower($val);
        $item=strtolower($name);
        if($val==$name)
            $cnt+=1;
        }
    }
    }

    array_find($name,$coll);

    if($cnt==0)
    echo "$name was Not Found";
    else
    echo "$name was found $cnt times.";
share|improve this question
 
what is your question ? –  Nambi Narayanan Mar 7 at 6:05
 
want to search a name and count the repetitions. –  Always_Cool Mar 7 at 6:06
 
victor was found 2 times. isn't that right ? –  Shankar Damodaran Mar 7 at 6:07
 
and 1st of all, how the hell to edit that code. i've wasted an hour or so and still couldn't get with it !! –  Always_Cool Mar 7 at 6:07
1  
@ShankarDamodaran thanks a lot man !! :) –  Always_Cool Mar 7 at 6:14
show 8 more comments

1 Answer

up vote 1 down vote accepted

Try this code, hope it will work

<?php
static $cnt = 0;
$name = 'victor';
$coll = array(
    'dep1' => array(
        'fy' => array('john', 'johnny', 'victor'),
        'sy' => array('david', 'arthur'),
        'ty' => array('sam', 'joe', 'victor')
    ),
    'dep2' => array(
        'fy' => array('natalie', 'linda', 'molly'),
        'sy' => array('katie', 'helen', 'sam', 'ravi', 'vipul'),
        'ty' => array('sharon', 'julia', 'maddy')
    )
);

function recursive_search(&$v, $k, $search_query){
    global $cnt;
    if($v == $search_query){
        ++$cnt;
    }
}
array_walk_recursive($coll, 'recursive_search' , $name);
if ($cnt == 0)
    echo "$name was Not Found";
else
    echo "$name was found $cnt times.";

DEMO

share|improve this answer
 
yes, that is very helpful. Now how could i get the intermediate array name i.e. whether the name was found in "dep1" or "dep2" ? –  Always_Cool Mar 7 at 7:27
 
@Always_Cool, this function will work in any level. –  Girish Mar 7 at 7:31
 
@Always_Cool, array with 4rd level eval.in/115161 –  Girish Mar 7 at 7:32
 
sry but i think u didn't got me right. i need to find the sub array name in which the name(s) was found. like victor was found in "dep1,dep2" sorry again if this que is pretty stupid to be asked :( –  Always_Cool Mar 7 at 7:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.