7

I have converted an xml object returned from a php function into json format to send it to js file like.

function searchResults($q) { ...
    $xml = simplexml_load_string($result);
    return json_encode($xml); }

I receive/use it in js like

  var msg_top = "<"+"?php echo searchResults('windows');"+"?"+">";

Then I receive it back in php & decoded.

      $json = $_POST['msg_top'];
      $msg = json_decode($json);

Now how do I loop through it to get all values of its certain properties that I could have get from xml object(which I converted into json). This is how I loop over xml object to get all values of its certain properties:

   foreach ($xml->entry as $status) {
   echo $status->author->name.''.$status->content);
   }

How do I get all those values from decoded json object $msg?
EDITED I tried in same HTML where I am using js to receive & POST php search function data via ajax, I tried following code to loop through json in php. But it did not show anything.

$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}
5
  • are you actually trying to execute php from js? nice idea, but will not work... Commented Nov 23, 2010 at 9:50
  • 1
    @joni, thanX for the reply. May be something in my post confuses u. Not really, I just need to access some php results in js, use those there & send back again to php server for further use. Commented Nov 23, 2010 at 9:59
  • but how does "<"+"?php echo searchResults('windows');"+"?"+">" get executed on PHP runtime?? Commented Nov 23, 2010 at 10:21
  • Is it not like that whenever this function is called, it will return results? As my need is to get results in js that this php function returns. So I am calling it using php tags in js Commented Nov 23, 2010 at 10:52
  • you cannot call a php func from js, except using ajax. And please use a @joni if you answer to me, so I get notified =) Commented Nov 24, 2010 at 9:45

2 Answers 2

11

By default, json_decode returns an stdClass. stdClass-es can be used the same way as associative arrays with foreach.

Alternatively, you can ask json_decode to return an associative array:

$array = json_decode($_POST['foo'], TRUE);
Sign up to request clarification or add additional context in comments.

Comments

0

I think you have to use $msg for the FOR LOOP as it is the array.

Try to see what it hold using this

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

1 Comment

I am just saying this as $status is used in FOR LOOP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.