1

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]
11
  • Have you tried writing your own recursive function that does this? Commented Jan 27, 2014 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 Commented Jan 27, 2014 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 Commented Jan 27, 2014 at 15:22
  • 2
    @dainis abols if no function exists I'll write my own code Commented Jan 27, 2014 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 Commented Jan 27, 2014 at 15:26

1 Answer 1

0

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]
Sign up to request clarification or add additional context in comments.

Comments

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.