1

I've been trying to write a PHP function which searches the id index valeus in array, and once found, returns the path which lead to it's discovery.

Take the following array:

    Array
(
    [0] => Array
        (
            [id] => 1
            [data] => Array
                (
                    [0] => Array
                        (
                            [id] => 8
                        )

                    [1] => Array
                        (
                            [id] => 9
                        )

                    [2] => Array
                        (
                            [id] => 10
                            [data] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 15
                                            [data] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 22
                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 21
                                        )

                                )

                        )

                )

        )

)

If looking for [id] => 21 it would return array(1,10). However, in numerous attempts I have failed. The set path should be set to the index id. However, I cannot figure it out. Any words of guidance are much appreciated.

0

2 Answers 2

2

This functions returns array(1,10) for OP example

(will leave that other answer just in case someone will look for normal "path searching")

function search_data($needle, $haystack) {
    if (is_array($haystack)) {
        foreach($haystack as $data) {
            if ($data['id'] == $needle) return array();
            if (isset($data['data'])) {
                if (($path = search_data($needle, $data['data'])) !== false) return array_merge(array($data['id']), $path);
            }
        }
    }
    return false;    
}
Sign up to request clarification or add additional context in comments.

1 Comment

It works! I solved it a few seconds ago, but you're example does the trick a lot faster with less code. Thank you. :3
1

This functions returns array(0,'data',2,'data',1,'id') for OP example (i.e. full path to value)

Function which searches for $key => $value pair in array and returns the path:

function array_search_r($key, $value, $haystack, $strict = null) {
    $strict = $strict ?: false;
    if (is_array($haystack)) {
        foreach($haystack as $k => $v) {
            if ($strict ? ($k === $key && $v === $value) : ($k == $key && $v == $value)) return array($k);
            if(($path = array_search_r($key, $value, $v, $strict)) !== false) return array_merge(array($k), $path);
        }
    }
    return false;
}

2 Comments

It returns array(1,10) because the parent arrays have id set to first 1 then 10. I will test your function out, thanks.
Ok I see now what you mean, see my other answer.

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.