Home  >  Article  >  Backend Development  >  How to Access Values from Multi-Dimensional PHP Arrays?

How to Access Values from Multi-Dimensional PHP Arrays?

Linda Hamilton
Linda HamiltonOriginal
2024-11-03 22:15:03306browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn