Home > Article > Backend Development > How to Access Values from Multi-Dimensional PHP Arrays?
How to Access Values from Multi-Dimensional PHP Arrays
You're confronted with a multi-dimensional PHP array, as shown by print_r($myarray), and you're eager to retrieve specific values like the email or gender.
Solution:
To access values within a multi-dimensional array, navigate through its dimensions by specifying the array key at each level. For example:
<code class="php">echo $myarray[0]['email']; // prints "[email protected]" echo $myarray[0]['gender']; // prints "male"</code>
Examine the keys and indentations in the output of print_r:
<code class="php">$myarray = Array ( [0] => Array ( [id] => 6578765 [name] => John Smith [first_name] => John [last_name] => Smith [link] => http://www.example.com [gender] => male [email] => [email protected] [timezone] => 8 [updated_time] => 2010-12-07T21:02:21+0000 ) )</code>
You can deduce that the first element ([0]) is a subarray containing the user data, and its keys specify the attributes (e.g., email, gender).
The above is the detailed content of How to Access Values from Multi-Dimensional PHP Arrays?. For more information, please follow other related articles on the PHP Chinese website!