Solved

parsing JSON in php

Posted on 2010-09-17
3
404 Views
Last Modified: 2013-11-08
So i am trying to get the script below to work, i want to echo out all the id's so something like

st jo

but i cannot get it to work, i get an error, my foreach loop has something wrong in it
<?php
  
  $orig = '[{"id": "st","name": "steve"},{"id": "jo","name": "joey"}]';

$json = json_decode($orig);

echo $json[0]->id;

foreach($json->id as $val)
{
    echo($val);
}

//var_dump($json);
  
  
?>

Open in new window

0
Comment
Question by:Brant Snow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Author Comment

by:Brant Snow
ID: 33703355
actually ideally would be a way to echo out all the results of the object like

foreach($json as $val)
{
echo $val->id;
echo $val->name;

}

so i would get something like

st steve jo joey
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 150 total points
ID: 33703422
Here you go



$orig = '[{"id": "st","name": "steve"},{"id": "jo","name": "joey"}]';
$json = json_decode($orig);

foreach($json as $val)
{
	echo $val->id . " - " . $val->name;
}

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 350 total points
ID: 33707869
The code snippet outputs this, thus proving that ChrisStanyon's solution works.  You can also get json_decode() to give you an array (second part of the example).

If you use var_dump() to print out objects and arrays, you will find that it is very helpful!

Best, ~Ray
---
array(2) {
  [0]=>
  object(stdClass)#1 (2) {
    ["id"]=>
    string(2) "st"
    ["name"]=>
    string(5) "steve"
  }
  [1]=>
  object(stdClass)#2 (2) {
    ["id"]=>
    string(2) "jo"
    ["name"]=>
    string(4) "joey"
  }
}

st == steve
jo == joey
array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    string(2) "st"
    ["name"]=>
    string(5) "steve"
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(2) "jo"
    ["name"]=>
    string(4) "joey"
  }
}

st == steve
jo == joey

<?php // RAY_temp_thawts.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;



// TEST DATA FROM THE POST AT EE
$json = '[{"id": "st","name": "steve"},{"id": "jo","name": "joey"}]';



// TRY TO DECODE IT.  MAN PAGE: http://us2.php.net/manual/en/function.json-decode.php
$obj = json_decode($json);

// USE DATA VISUALIZATION TO SEE WHAT WE GOT
var_dump($obj);

// USE AN ITERATOR TO PROCESS THE OBJECT
foreach($obj as $val)
{
    echo PHP_EOL . $val->id . " == " . $val->name;
}
echo PHP_EOL;


// ANOTHER WAY - USING AN ARRAY
$arr = json_decode($json, TRUE);

// USE DATA VISUALIZATION TO SEE WHAT WE GOT
var_dump($arr);

// USE AN ITERATOR TO PROCESS THE ARRAY
foreach($arr as $thing)
{
    echo PHP_EOL . $thing['id'] . " == " . $thing['name'];
}
echo PHP_EOL;

Open in new window

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question