-2

I have this Method in my class, which is working great, (kind-of). But for, whatever reason, it returns NULL. Althrough if I put a (print_r($keys)) in the function, it will print it.

The RETURN is not passing the values. Any thoughts?

    function arrayKeyPath ($searchFor, $arr, $keys=array())
    {
        if(!empty($arr) && is_array($arr)) {
            if(isset($arr[$searchFor])) {
                return $keys;
            }
            foreach($arr as $pKey => $a) {
                if(is_array($a)) {
                    $keys[] = $pKey;
                    arrayKeyPath($searchFor, $a, $keys);
                }
            }
        }
    }

    $user['results'][0] = array (
        'userId' => '1',
        'firstName' => 'John',
        'lastName' => 'Doe',
        'options' =>
            array (
                'showNews' => 'on',
                'newOptions' => array(
                    'option1'=> 1,
                    'option2'=> 2,
                    'option3'=> 3
                ),
                'connectWithTimeFrame1' => '30',
                'defaultMessageTemplate' => '12',
                'connectWithTimeFrame' => 90,
            ),
    );



$exists = arrayKeyPath('option1', $user['results'][0]);
var_dump($exists);

Online Run Version https://ideone.com/fnml1S

Justin
  • 2,502
  • 7
  • 42
  • 77
  • 1
    There is no `option1` key on the first level so the `return` is never hit – PeeHaa Dec 08 '15 at 20:07
  • It continues to re-itterate through the loop, it does get hit. If you place a Print_R($keys), just before the return, it will display what it's supposed to. – Justin Dec 08 '15 at 20:10
  • No it doesn't get hit. It continues execution and somewhere *in your recursives calls it might get hit*, but nothing is returned at that point on the first function call. You just ignore the result of your further function calls – PeeHaa Dec 08 '15 at 20:11
  • Shouldn't the second argument be `$user['results'][0]['options']`? – frz3993 Dec 08 '15 at 20:13
  • FWIW I would suggest using your debugger and stepping through your code to see what is happening. – PeeHaa Dec 08 '15 at 20:14
  • `$keys` never return, the `option1` just make it loop until the end of array – Andrew Dec 08 '15 at 20:20
  • Here is an online CodePen basically, it runs, and it does hit th $keys, https://ideone.com/fnml1S but just doesn't return.. – Justin Dec 08 '15 at 20:22
  • sry, I see your point now – Andrew Dec 08 '15 at 20:37
  • @Justin it does hit it somewhere deeper in the stack but IT NEVER RETURNS to the first call – PeeHaa Dec 08 '15 at 20:40

1 Answers1

0

So what you are basically trying to do is searching for a key in a multidimensional array. If you just want to check if the key is there at all, consider the following code:

// shamelessly copied from http://codeaid.net/php/extract-all-keys-from-a-multidimensional-array
function array_keys_multi(array $array) {
    $keys = array();
    foreach ($array as $key => $value) {
        $keys[] = $key;
        if (is_array($value))
            $keys = array_merge($keys, array_keys_multi($value));
    }
    return $keys;
}

// in your case:
// array("results", 0, "userId", "firstName", "lastName", "options", "showNews", "newOptions", "option1", "option2", "option3", "connectWithTimeFrame1", "defaultMessageTemplate", "connectWithTimeFrame");

// call the function
$keys = array_keys_multi($user);

// check if the "option1" can be found in the array
if (in_array("option1", $keys))
    echo "Yup, in there.";
Jan
  • 42,290
  • 8
  • 54
  • 79
  • 1
    Then maybe this is for you: http://stackoverflow.com/questions/27626880/searching-for-key-in-multidimensional-array-and-returning-path-to-it – Jan Dec 08 '15 at 20:31