Home > Backend Development > PHP Tutorial > How Do I Access Elements in Arrays and Objects in PHP?

How Do I Access Elements in Arrays and Objects in PHP?

Barbara Streisand
Release: 2025-01-06 03:25:39
Original
370 people have browsed it

How Do I Access Elements in Arrays and Objects in PHP?

Accessing Arrays and Objects

Arrays

To access an array element, you use square brackets []. For example:

$array = ['foo', 'bar', 'baz'];
echo $array[0]; // prints 'foo'
Copy after login

Objects

To access an object property, you use the arrow operator ->. For example:

$object = new stdClass;
$object->foo = 'bar';
echo $object->foo; // prints 'bar'
Copy after login

Nested Arrays and Objects

If you have nested arrays or objects, you can use the above operators to access them. For example:

$array = [
    [1, 2, 3],
    [4, 5, 6]
];
echo $array[0][1]; // prints '2'

$object = new stdClass;
$object->foo = new stdClass;
$object->foo->bar = 'baz';
echo $object->foo->bar; // prints 'baz'
Copy after login

Example

In your specific case:

$get_user = [
    0 => 10499478683521864,
    1 => '07/22/1983',
    2 => '[email protected]',
    3 => 'Alan',
    4 => 'male',
    5 => 'Malmsteen',
    6 => 'https://www.facebook.com  app_scoped_user_id/1049213468352864/',
    7 => (object) [
        'id' => 102173722491792,
        'name' => 'Jakarta, Indonesia'
    ],
    8 => 'id_ID',
    9 => 'El-nino',
    10 => 'Alan El-nino Malmsteen',
    11 => 7,
    12 => '2015-05-28T04:09:50+0000',
    13 => 1
];
Copy after login

To access the email address, you can use:

$email = $get_user[2];
Copy after login

The above is the detailed content of How Do I Access Elements in Arrays and Objects in PHP?. 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