Why doesn't this PHP recursive function return the value?

后端 未结 4 1548
眼角桃花
眼角桃花 2020-12-12 02:03


I was working on an API for ustream which returns the following array

Array
(
[results] => Array
    (
        [0] => Array
            (
                 


        
相关标签:
4条回答
  • 2020-12-12 02:39

    You're not doing anything with the return from your recursion call. It's a bit more complex than simply returning it, because you only want to cascade out if we actually found anything.

    Try this:

    function recursion($array) {
        foreach ($array as $key => $value) {
            if($key==='sourceChannel') {
                return $value['id'];
            }
            if (is_array($value)) {
                $rc = recursion($value);
                if (!is_null($rc)) return $rc;
            }
        }
        return null;
    }
    

    First, I added return null; to the end of the function - if we didn't find anything, we return null.

    Then, when I recurse, I check the return value... if it's non-null, it means we found something and we want to return it. If it is null, we don't want to return - we want to continue the loop instead.

    0 讨论(0)
  • 2020-12-12 02:54

    You could use iterators to make your function a whole lot easier:

    function findSourceChannel($array)
    {
        foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key => $value) {
            if ($key == 'sourceChannel') {
                return $value['id'];
            }
        }
        return null;
    }
    

    That said, I'm not sure why you can't just do a search like this:

    foreach ($array['results'] as $result) {
        if (isset($result['sourceChannel'])) {
            return $result['sourceChannel']['id'];
        }
    }
    return null;
    
    0 讨论(0)
  • 2020-12-12 03:04

    Your code is flawed because you need to change

    recursion($value);
    

    into

    return recursion($value);
    

    The return will take the answer back to the caller until the first caller is reached.

    edit: The lesson here is that your function should ALWAYS return a value. So here is some more elaboration:

    function recursion($array) {
        if (!is_array($array)) // check if what you're getting is an array!
            return null;
        foreach ($array as $key => $value) {
            if($key==='sourceChannel') {
                return $value['id'];
            }
            if (is_array($value)) {
                $result = recursion($value);
                if ($result !== null) {
                    return $result;
                }
            }
        }
        return null; // this happens if all recursions are done and sourceChannel was not found
    }
    
    0 讨论(0)
  • 2020-12-12 03:04

    Try like this

    foreach ($array['results'] as $key => $value) {
       if($key == "sourceChannel"){
            $getId = $value['id'];
            //print_r($getId);
            return $getId;
       }       
    }
    
    0 讨论(0)
提交回复
热议问题