1

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

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
You need to post your whole array or you could search SO for a lot of topics on recursive search in arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.