如何在php文件中循环使用这个json对象?

如何在php文件中循环使用这个json对象?,php,json,loops,decode,Php,Json,Loops,Decode,我已将php函数返回的xml对象转换为json格式,以便将其发送到js文件,如 function searchResults($q) { ... $xml = simplexml_load_string($result); return json_encode($xml); } 我在js中接收/使用它,就像 var msg_top = "<"+"?php echo searchResults('windows');"+"?"+">"; 现在,我如何循环通过它来

我已将php函数返回的xml对象转换为json格式,以便将其发送到js文件,如

function searchResults($q) { ...
    $xml = simplexml_load_string($result);
    return json_encode($xml); }
我在js中接收/使用它,就像

  var msg_top = "<"+"?php echo searchResults('windows');"+"?"+">";
现在,我如何循环通过它来获得它的某些属性的所有值,这些值可以从xml对象(我将其转换为json)中获得。这就是我循环xml对象以获取其特定属性的所有值的方式:

   foreach ($xml->entry as $status) {
   echo $status->author->name.''.$status->content);
   }
如何从解码的json对象$msg获取所有这些值?
已编辑 我尝试在同一个HTML中使用js通过ajax接收和发布php搜索函数数据,我尝试以下代码在php中循环使用json。但它没有显示任何东西

$obj = searchResults(testword);//serach function returns json encoded data
$obj = json_decode($obj, true);
$count = count($obj);  
for($i=0;$i<$count;$i++)
{
echo $obj[$i][content];}// using xml for it, I get ouput like foreach ($xml3->entry as 
                       // $status) {status->content}
$obj=搜索结果(testword)//serach函数返回json编码的数据
$obj=json_decode($obj,true);
$count=计数($obj);
对于($i=0;$ientry作为
//$status){status->content}

默认情况下,
json\u decode
返回stdClass。stdClass es的使用方法与使用
foreach
的关联数组相同

或者,您可以要求
json\u decode
返回关联数组:

$array = json_decode($_POST['foo'], TRUE);

我认为您必须对for循环使用
$msg
,因为它是数组

用这个试试看它能装什么

echo "<pre>".print_r($msg)."</pre";
//And if you see the correct array structure
foreach($msg as $key=>$value) {
  //do your things
}

echo”“.print_r($msg)。“你真的在尝试从js执行php吗?好主意,但不起作用…@joni,thanX的回复。可能是我帖子中的某些内容让你困惑。不是真的,我只需要访问js中的一些php结果,使用这些结果并再次发送回php服务器以供进一步使用。但是
是怎么做的?”“
get executed on PHP runtime”?是不是每次调用此函数时都会返回结果?因为我需要的是在js中得到这个php函数返回的结果。所以我在js中使用php标记调用它。除了使用ajax,您不能从js调用php func。如果你回答我,请使用@joni,这样我会得到通知=)我这么说是因为FOR循环中使用了
$status
echo "<pre>".print_r($msg)."</pre";
//And if you see the correct array structure
foreach($msg as $key=>$value) {
  //do your things
}