Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.