我不明白如何从函数返回json。这是我试过的代码:从函数返回json并传递给变量
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];
}
它是如何工作的,我试过很多东西,但计数$ json_res为0每次?对不起,我没有那么多的经验:)
编辑:“$ json_source”在函数buildJson()中生成。我测试了它存在于“echo count($ res); // 6”。我可以访问函数中的$ res的所有值,但是我无法返回并在外部访问它。所以“$ json_source”不是问题。任何其他想法?
已解决:好吧,现在我发现了这个问题,出于编码的原因,我在buildJson()中调用了buildJson()函数。现在我用另一种方式编码它,现在它工作。谢谢@all :)
buildJson参数是缺少 –
阅读:http://stackoverflow.com/help/mcve – walther
我不能提供$ json_source这是一个HTTP分析。 – Mona935