为什么这个PHP递归函数没有返回值?

时间:2013-06-05 07:01:22

标签: php arrays function recursion


我正在为ustream开发一个API,它返回以下数组

Array
(
[results] => Array
    (
        [0] => Array
            (
                [id] => 33756327
                [userName] => sachintaware
                [title] => Mobile record
                [protected] => 
                [description] => Recorded on my Android phone.
                [createdAt] => 2013-06-03 03:29:38
                [rating] => 0.000
                [lengthInSecond] => 371.544
                [totalViews] => 5
                [codecIsForLiveHttp] => 
                [url] => http://www.ustream.tv/recorded/33756327
                [embedTag] => 
                [liveHttpUrl] => 
                [imageUrl] => Array
                    (
                        [small] => http://static-cdn2.ustream.tv/videopic/0/1/33/33756/33756327/1_14659377_33756327_120x90_b_1:2.jpg
                        [medium] => http://static-cdn2.ustream.tv/videopic/0/1/33/33756/33756327/1_14659377_33756327_320x240_b_1:2.jpg
                    )

                [sourceChannel] => Array
                    (
                        [id] => 14659377
                        [url] => http://www.ustream.tv/channel/14659377
                    )

            )

        [1] => Array
            (
                [id] => 33756481
                [userName] => sachintaware
                [title] => gobiggitest
                [protected] => 
                [description] => gobiggitest
                [createdAt] => 2013-06-03 03:37:49
                [rating] => 0.000
                [lengthInSecond] => 647.580
                [totalViews] => 11
                [codecIsForLiveHttp] => 
                [url] => http://www.ustream.tv/recorded/33756481
                [embedTag] => 
                [liveHttpUrl] => 
                [imageUrl] => Array
                    (
                        [small] => http://static-cdn2.ustream.tv/videopic/0/1/33/33756/33756481/1_14659377_33756481_120x90_b_1:3.jpg
                        [medium] => http://static-cdn2.ustream.tv/videopic/0/1/33/33756/33756481/1_14659377_33756481_320x240_b_1:3.jpg
                    )

                [sourceChannel] => Array
                    (
                        [id] => 14659377
                        [url] => http://www.ustream.tv/channel/14659377
                    )

            )

这是我用来浏览数组的函数,因为我想获取[id] => 14659377里面的sourceChannel。但是我得到一个空值作为返回值。这是什么错误我正在做?有人可以帮我吗?

$getUsername = array();
function recursion($array) {
foreach ($array as $key => $value) {
      if($key==='sourceChannel'){
            $getId = $value['id'];
            //print_r($getId);
            return $getId;
          }
      if (is_array($value))
         recursion($value);
     }
}

$getUsername=recursion($resultsArray);
print_r($getUsername);

4 个答案:

答案 0 :(得分:4)

您的代码存在缺陷,因为您需要更改

recursion($value);

进入

return recursion($value);

返回将把答案带回呼叫者,直到到达第一个呼叫者。

修改 这里的教训是你的函数应该总是返回一个值。所以这里有一些更详细的说明:

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
}

答案 1 :(得分:1)

试试这个

foreach ($array['results'] as $key => $value) {
   if($key == "sourceChannel"){
        $getId = $value['id'];
        //print_r($getId);
        return $getId;
   }       
}

答案 2 :(得分:1)

你从递归调用返回时没有做任何事情。 它比简单地返回它要复杂一点,因为如果我们真的发现了什么,你只想要级联出来。

试试这个:

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;
}

首先,我将return null;添加到函数的末尾 - 如果我们没有找到任何内容,则返回null。

然后,当我递归时,我检查返回值...如果它是非空的,这意味着我们找到了一些东西,我们想要返回它。如果它为null,我们想要返回 - 我们希望继续循环。

答案 3 :(得分:1)

您可以使用迭代器使您的函数更容易:

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

那就是说,我不确定你为什么不能这样做一个搜索:

foreach ($array['results'] as $result) {
    if (isset($result['sourceChannel'])) {
        return $result['sourceChannel']['id'];
    }
}
return null;