How to Access Values from Multi-Dimensional PHP Arrays?

Linda Hamilton
Release: 2024-11-03 22:15:03
Original
298 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!