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
Barbara StreisandOriginal
2025-01-06 03:25:39412browse

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'

Objects

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

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

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'

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
];

To access the email address, you can use:

$email = $get_user[2];

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!

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