MK27 Posted June 11, 2010 Share Posted June 11, 2010 Hi. I'm brand new to php but have lots of experience with other similar interpreted languages. I'm trying to take apart some JSON output from a semantic mediawiki. So I have an associative array created with json_decode, and to view it (nb, several times in different ways): <pre> <?php print_r($json); foreach ($json['items'] as $items) { foreach ($items as $entry) { print_r($entry); print "<p>-->".$entry['shortdesc']."<--</p>"; } } ?> </pre> Now here's sample of the print_r("$json") output: Array ( [properties] => Array ( [shortdesc] => Array ( [valueType] => text ) ) [items] => Array ( [0] => Array ( [label] => Bingo-cards [shortdesc] => Creates number, word/letter, and picture bingo cards [uri] => http://localhost/wiki/index.php?title=Bingo-cards ) [1] => Array ( [label] => Claroline [shortdesc] => Online learning system [uri] => http://localhost/wiki/index.php?title=Claroline ) ) ) So what I see here is that the $json array has two members, "properties" and "items". "Items" is an array of associative arrays, right? (With members "label", "uri", and "shortdesc".) But the output from this part of that code: foreach ($items as $entry) { print_r($entry); print "<p>-->".$entry['shortdesc']."<--</p>"; } Does this: Bingo-cards -->B<-- Creates number, word/letter, and picture bingo cards -->C<-- http://localhost/wiki/index.php?title=Bingo-cards -->h<-- Claroline -->C<-- Online learning system -->O<-- http://localhost/wiki/index.php?title=Claroline -->h<-- See what's happening? The "items" array is printing as if it were a non-associative, 1D array with the "label", "uri", and "shortdesc" lines alternating. The the "shortdesc" for that is evidentally it's first letter?!!??? I suppose I can still iterate thru this and do what I want, but (being brand new to php) I cannot get these two forms of output to jibe in my mind. Why does each element of "items" appear to be an associative array, and then it isn't? Nb. this: print $json['items'][0]['shortdesc']; Does print the shortdesc but again: foreach ($json['items'] as $items) { foreach ($items as $entry) { print "<p>-->".$entry['shortdesc']."<--</p>"; } } Does not. Like, WTF? Link to comment https://forums.phpfreaks.com/topic/204490-accessing-associative-array-depths/ Share on other sites More sharing options...
MK27 Posted June 11, 2010 Author Share Posted June 11, 2010 Sheesh. Sometimes I'd swear I just can't see straight. Anyway found my silly error... Link to comment https://forums.phpfreaks.com/topic/204490-accessing-associative-array-depths/#findComment-1070804 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.