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.

when searching an element in a nested array, could i get back it's 1st level nesting index.

<?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){
       /* i want the sub array index to be returned */
    }
}

?>

i.e to say, if i'am searching 'victor', i would like to have 'dep1' as the return value. Could anyone help ??

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Try:

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

$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($coll), RecursiveIteratorIterator::SELF_FIRST);

/* These will be used to keep a record of the
   current parent element it's accessing the childs of */
$parent_index = 0;
$parent = '';
$parent_keys = array_keys($coll); //getting the first level keys like dep1,dep2
$size = sizeof($parent_keys);
$flag=0; //to check if value has been found

foreach ($iter as  $k=>$val) {
    //if dep1 matches, record it until it shifts to dep2
    if($k === $parent_keys[$parent_index]){ 
        $parent = $k;
            //making sure the counter is not incremented
            //more than the number of elements present
        ($parent_index<$size-1)?$parent_index++:'';
    }
    if ($val == $name) {
        //if the value is found, set flag and break the loop
        $flag = 1;
        break;
    }
}

($flag==0)?$parent='':''; //this means the search string could not be found
echo 'Key = '.$parent;

Demo

share|improve this answer

This works , but I don't know if you are ok with this...

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

foreach($col2 as $k=>$arr)
{
    foreach($arr as $k1=>$arr2)
    {
        if(in_array($name,$arr2))
        {
            echo $k;
            break;
        }
    }
}

OUTPUT :

dept2

Demo

share|improve this answer
    
your code work, thats all fine. but in case my array is of 3 of 4 nested levels, it wont work. actually finding a solution that will work on any level of nesting –  Always_Cool Mar 10 at 8:05
    
You need to post your whole array or you could search SO for a lot of topics on recursive search in arrays. –  Shankar Damodaran Mar 10 at 8:13

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.