wubwubwub Posted May 22, 2012 Share Posted May 22, 2012 Hello. I have a newbie question regarding arrays: How can I echo an element's subsequent arrays and elements? For example $pers = array("Steve" => array("eyes" => "green", "age" => "43", "race" => "caucasian")); and if I echo $pers["Steve"] to make it display: green 43 caucasian. Thanks. Link to comment https://forums.phpfreaks.com/topic/262912-simple-array-question/ Share on other sites More sharing options...
Alex Posted May 22, 2012 Share Posted May 22, 2012 Well, it's a multidimensional array so $pers["Steve"] itself is an array, so to add one of the elements you'd do something like: $pers["Steve"]["eyes"] For the specific output you asked you'd do something like: echo $pers["Steve"]["eyes"] . " " . $pers["Steve"]["age"] . " " . $pers["Steve"]["race"]; Link to comment https://forums.phpfreaks.com/topic/262912-simple-array-question/#findComment-1347531 Share on other sites More sharing options...
scootstah Posted May 22, 2012 Share Posted May 22, 2012 echo $pers["Steve"]["eyes"] . " " . $pers["Steve"]["age"] . " " . $pers["Steve"]["race"]; This should work too: echo implode(' ', $pers['Steve']); Link to comment https://forums.phpfreaks.com/topic/262912-simple-array-question/#findComment-1347551 Share on other sites More sharing options...
Alex Posted May 22, 2012 Share Posted May 22, 2012 echo $pers["Steve"]["eyes"] . " " . $pers["Steve"]["age"] . " " . $pers["Steve"]["race"]; This should work too: echo implode(' ', $pers['Steve']); Yeah, in that case that will work just as well, but you might want to be careful. In general associative arrays aren't required to have an associated order. With PHP order will be maintained in associative arrays since they're implemented as essentially being hash tables, but you shouldn't assume this in general. For example, if you want to achieve associative array functionality in JavaScript you're going to have to use objects (which aren't "real" associative arrays for other reasons but that's besides the point in this example) which are an unordered set of name/value pairs, so it's not guaranteed that order will be preserved. So imagine that this array was coming from JSON data, you have no real way of ensuring the order is going to be what you expect, so the output might not be in the order you were after. Link to comment https://forums.phpfreaks.com/topic/262912-simple-array-question/#findComment-1347561 Share on other sites More sharing options...
scootstah Posted May 22, 2012 Share Posted May 22, 2012 echo $pers["Steve"]["eyes"] . " " . $pers["Steve"]["age"] . " " . $pers["Steve"]["race"]; This should work too: echo implode(' ', $pers['Steve']); Yeah, in that case that will work just as well, but you might want to be careful. In general associative arrays aren't required to have an associated order. With PHP order will be maintained in associative arrays since they're implemented as essentially being hash tables, but you shouldn't assume this in general. For example, if you want to achieve associative array functionality in JavaScript you're going to have to use objects (which aren't "real" associative arrays for other reasons but that's besides the point in this example) which are an unordered set of name/value pairs, so it's not guaranteed that order will be preserved. So imagine that this array was coming from JSON data, you have no real way of ensuring the order is going to be what you expect, so the output might not be in the order you were after. Very true, thanks for pointing that out. Link to comment https://forums.phpfreaks.com/topic/262912-simple-array-question/#findComment-1347562 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.