How to Access Array and Object Elements in PHP
Arrays
To access elements in an array, use the square brackets ([]):
$array[0]; // Accesses the first element $array[2]; // Accesses the third element
If the array has nested arrays, use multiple square brackets to access nested elements:
$array["nested_array"][0]; // Accesses the first element of the nested array
Objects
To access properties in an object, use the arrow operator (->):
$object->property; // Accesses the "property" property $object->another_property; // Accesses the "another_property" property
If the object has nested objects, use multiple arrows to access nested properties:
$object->nested_object->property; // Accesses the "property" property of the nested object
Example
From your provided array, to access the element "[email protected]", you would do the following:
$get_user[2]; // Outputs [email protected]
Note on Hidden Characters
Sometimes, there may be hidden characters in the array or object key that are not visible in the browser. These characters can affect your ability to access the element. Use var_dump() to see the actual key and any hidden characters.
The above is the detailed content of How Do I Access Array and Object Elements in PHP?. For more information, please follow other related articles on the PHP Chinese website!