从函数返回json并传递给变量

I don't get it how to return a json from a function. Here's the code I tried:

function buildJson(){
    ... $json_source is parsed by http (this works)
    $source = json_decode($json_source, true);
    $res = $source['child'];
    echo count($res); // 6

    return $res;
    //return json_encode($res);
}

//////

$json_res = buildJson();
echo count($json_res); // 0

for($i = 0; $i < count($json_res); $i++){
     $item = $json_res[$i];
}

How does it work, I tried so many things but count for $json_res is 0 everytime? Sorry I don't have that much experience :)

EDIT: "$json_source" is generated inside the function buildJson(). I tested that it exists with "echo count($res); // 6". And I can access all the values of $res inside the function, but I can't return and access it outside. So "$json_source" is not the problem. Any other ideas?

SOLVED: Ok now I found the problem, for coding reasons I was calling buildJson() another time inside buildJson(). Now I coded it in another way and it works now. Thanks @all :)

Try to pass parameter to your function. I think its not working because you don't have a data source for your json

function buildJson($json_source){
    $source = json_decode($json_source, true);
   $res = $source['child'];
   echo count($res); // 6

   return $res;
  //return json_encode($res);
}



$json_res = buildJson($json_source);
echo count($json_res); // 0

for($i = 0; $i < count($json_res); $i++){
  $item = $json_res[$i];
}

Try this Code.

 $json_res = buildJson();
 $json_res = json_decode($json_res,true);
  echo count($json_res); 

 for($i = 0; $i < count($json_res); $i++){
   $item = $json_res[$i];
 }