Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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


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

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
                    )

            )

This is the function I used to go through the array as I want to fetch the [id] => 14659377 which is inside sourceChannel.But I am getting an empty value as a return value.What is the mistake I am making? can anyone please help me with it?

$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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...