Jump to content

Simple array question


wubwubwub

Recommended Posts

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

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"];

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.