Accessing array elements in PHP

Accessing array elements in PHP

To access an element in a PHP array, you can use the array’s index, which is the position of the element within the array. The index can be either a numerical value or a string.

For example, to access the first element in a numerically indexed array, you would use the following syntax:

$myArray = array("apple", "banana", "orange"); echo $myArray[0]; // Output: apple

To access an element in an associative array (where elements are stored as key-value pairs), you would use the key instead of the index:

$myAssocArray = array("name" => "John", "age" => 30, "city" => "New York"); echo $myAssocArray["name"]; // Output: John You can also use a loop (such as a foreach loop) to iterate through all elements in an array and access them one by one.

Apply for PHP Certification!

https://www.vskills.in/certification/certified-php-developer

Back to Tutorials

Specifying array – PHP
Creating/modifying Array

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?