0

My question: How can I break up and iterate through the JSON array pictured below?

I am making an AJAX web app and I need to serialize an array of objects in Javascript and put them in a url to pass to a php script. This is all going fine and the php script recieves the JSON like so..

$passed = $_GET['result']; 
if(isset($passed)){

$passed = str_replace("undefined" , " " , $passed); /*had to add this to remove the undefined value*/

$json = json_decode(stripslashes($passed));
echo"<br/>";
var_dump($json ); //this is working and dumps an array
}

When I call var_dump on the decoded JSON I echo an output like so...

array(1) { [0]=> object(stdClass)#70 (2) { ["itemCount"]=> int(0) ["ItemArray"]=> array(2) { [0]=> object(stdClass)#86 (6) { ["itemPosition"]=> int(0) ["planPosition"]=> int(0) ["Name"]=> string(5) "dsfsd" ["Description"]=> string(3) "sdf" ["Price"]=> string(0) "" ["Unit"]=> string(0) "" } [1]=> object(stdClass)#85 (6) { ["itemPosition"]=> int(1) ["planPosition"]=> int(0) ["Name"]=> string(4) "fdad" ["Description"]=> string(3) "sdf" ["Price"]=> string(0) "" ["Unit"]=> string(0) "" } } } }

The JSON This is the JSON I am receiving. It seems like some of the pairs don't have names? How can I access elements in this Array?

Thanks alot guys

The Json I would like to parse in PHP

4
  • 2
    "It seems like some of the pairs don't have names?" ... WHAT in the BIG BLUE MARBLE are you talking about? Commented Feb 14, 2012 at 1:37
  • Okay fair call, my JSON knowledge is limited. Commented Feb 14, 2012 at 1:39
  • 1
    off topic, but what app/tool is that json parsed in.. thats purdy :) Commented Feb 14, 2012 at 2:58
  • @Mike - it was highlighted in the webapp json.parser.online.fr Commented Feb 16, 2012 at 23:04

2 Answers 2

3

Some of these elements are coming back as stdClass objects as you can see in the var_dump output. You can get at the attributes with the standard object notation, for example, with your $json variable:

echo $json[0]->itemCount; // 0
echo $json[0]->itemArray[0]->itemPostion; // 0

You can also iterate over stdClass instances just like any PHP object, you'll be looping through the public data members, so again with your $json:

foreach(echo $json[0]->itemArray[0] as $key => $value)
  echo 'key: ' . $key . ', value: ' . $value . PHP_EOL;

will loop through that first object, echoing out the member names and values of the object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that's very helpful - I'm new to php - more used to Java. Thanks for the time.
2

You just access them by index:

data[0] // first data item

Note that that's how you would "normally" access an array in the usual sense, so I might be missing something about your question here...

Comments

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.