tags:

views:

61

answers:

5

Here's the json

[{"location":"USA","email":"[email protected]","sex":"male","age":"Unkown","other":null,"profile":{"net":["55","56"],"networks":[{"site_url":"http://site.com","network":"test","username":"mike"},{"site_url":"http://site.com/2","network":"test2","username":"mike2"}]},"name":"Mike Jones","id":111}]

I wanted to know how I could echo out all networks so it echos out the site_url,network, and user for each of the 2.

How would I get "name" at the end out of there as well?

Tanks!

A: 

http://lt2.php.net/json_decode

GameBit
+4  A: 

Use json_decode() http://php.net/manual/en/function.json-decode.php

  $data = json_decode(...your sstring ...);
  echo $data[0]->name;
aviv
That's why for name, what about all the networks?
+3  A: 

Use json_decode to decode the JSON data. Then you can iterate the array with foreach and access the site_url‍s of each array item with another foreach like:

$arr = json_decode($json);
foreach ($arr as $obj) {
    foreach ($obj->profile->networks as $network) {
        echo $network->site_url;
    }
}
Gumbo
I'm not using PHP, but just for completeness sake: would the inner `foreach` be skipped gracefully if `$obj->profile` is actually undefined or `null`?
Arjan
@Arjan: No, it would raise two errors: One for that `$obj->profile` is not an object an thus there is no “networks” property; and one for that it’s not an array that is passed to `foreach`. So it would be better to check that before.
Gumbo
A: 

Building on aviv's answer...

$data = json_decode(...your sstring ...);
echo $data[0]->location; // USA
...
echo $data[0]->profile->net[0]; // 55
echo $data[0]->profile->net[1]; // 56
echo $data[0]->profile->networks[0]->site_url; // http://site.com
echo $data[0]->profile->networks[0]->network;  // test
echo $data[0]->profile->networks[0]->username; // mike
echo $data[0]->profile->networks[1]->site_url; // http://site.com/2
echo $data[0]->profile->networks[1]->network;  // test2
echo $data[0]->profile->networks[1]->username; // mike2
echo $data[0]->name; // Mike Jones
mkoistinen
`null` and `111` *are* valid values. See the syntax of [JSON](http://json.org/).
Gumbo
Oops! You're, of course, correct.
mkoistinen
A: 

Here's a straight-forward example doing the two things that you ask for (see inline comments).

$json = '[{"location":"USA","email":"[email protected]","sex":"male","age":"Unkown","other":null,"profile":{"net":["55","56"],"networks":[{"site_url":"http://site.com","network":"test","username":"mike"},{"site_url":"http://site.com/2","network":"test2","username":"mike2"}]},"name":"Mike Jones","id":111}]';

// "Decode" JSON into (dumb) native PHP object
$data = json_decode($json);

// Get the first item in the array (there is only one)
$item = $data[0];

// Loop over the profile.networks array
foreach ($item->profile->networks as $network) {
    // echos out the site_url,network, and user 
    echo "site_url = " . $network->site_url . PHP_EOL;
    echo "network  = " . $network->network . PHP_EOL;
    echo "user     = " . $network->username . PHP_EOL;
}

// Get "name" at the end
echo "name     = " . $item->name . PHP_EOL;

It should output (if you're viewing as HTML, it will be munged onto one line… don't output as HTML).

site_url = http://site.com
network  = test
user     = mike
site_url = http://site.com/2
network  = test2
user     = mike2
name     = Mike Jones
salathe