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.

I have the following array as input:

$test = array(
            array(
                'cat','d'=>'fox',
            ),
            'x' => array(
                'y'=> array('fox'),
            ),
        );

I need to find the sequence to the value "fox" in this array. Is there any function that would generate the sequence if given array as an input. ie. the output should be in the following format

$test[0]['d']
$test['x']['y'][0]
share|improve this question
 
Have you tried writing your own recursive function that does this? –  Dainis Abols Jan 27 at 15:19
 
What have you tried? All I see is an array with a standard method to obtain the sequence, no user created function –  Daryl Gill Jan 27 at 15:19
1  
I'm just searching whether there is any php functions that would enable me to generate the sequence. ie. we have print_r to display the entire array. I thought maybe a similar function might be present –  Ashok Gj Jan 27 at 15:22
2  
@dainis abols if no function exists I'll write my own code –  Ashok Gj Jan 27 at 15:23
2  
I think as of now, there is no library functions in php that gives exact sequence of array, although such functions are really useful for developers –  Ganesh Jan 27 at 15:26
show 6 more comments

1 Answer

After reading the comments, the answer is simple. You just asking if there is any built in PHP function which does this task.

Answer: No there is no built-in function for that, you have write your own. It should lead to an recursive algorithm. (or even iterative, as recursion can always be replaced by iteration).

Here comes a function which will do the job. Note that I've replaced recursion by iteration, in order to tweak the algorithm:

$a = array(
    array(
        'cat','d'=>'fox',
    ),
    'x' => array(
        'y'=> array('fox'),
    ),
);

function pathto($value, $array) {
    // init stack
    $stack = array(array($array, 'array'));
    do {
        // get first value from stack
        list ($current_value, $current_path) = array_shift($stack);

        // if current is a scalar value then compare to the input
        if($current_value === $value) {
            echo $current_path . PHP_EOL;
            continue;
        }

        // push array childs to stack
        if(is_array($current_value)) {
            foreach($current_value as $k => $v) {
                $k = is_string($k) ? "'$k'" : $k; 
                // push child and path to file
                array_push($stack, array (
                    $v, $current_path . '[' . $k . ']' 
                )); 
            }   
        }   
    } while (!empty($stack));
}

pathto('fox', $a);

Output:

$test[0]['d']
$test['x']['y'][0]
share|improve this answer
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.