access php array element

Access PHP Array Element

To access the PHP array element value, you can use the array index or the key which is enclosed in square brackets. Accessing elements of an Associative array can be done by key in PHP.

PHP comes with three different types of arrays to help you store data. You can either use simply indexed (numerical) arrays or you can create associative arrays or multidimensional arrays.

Indexed arrays are helpful when you want to just store a list of items.

Associative arrays are useful when you want to store key-value pairs like a list of customer IDs and the total value of products purchased by each one.

Multidimensional arrays are useful to store arrays in an array. This array can save an Indexed or Associative array or both. For example, you can store your customer list and their data in a multidimensional array.

In this tutorial, we are going to answer the questions about accessing the Indexed array, Associative array, and multidimensional array element value.

Access An Indexed array element in PHP

The index starts from 0 and increments by 1 from left to right of the array. So, the index of the first element is 0, the index of the second element is 1, the index of the third element is 2, and so on.

$cars = array("Ferrari","Benz","BMW","Volvo");

echo $cars[1];

Output:

Benz

In this example, we will take an array of strings, and access the second element using index 1.

Note: In PHP 8, you can use {} instead of []. Both ways work.

Accessing Elements of Associative PHP array value by key

To access the element value of an associative array in PHP you have to know the array keys. You can access a PHP Associative array element only by key.

$cars = array(
  "fer" => "Ferrari",
  "ben" => "Benz",
  "bmw" => "BMW",
  "vol" => "Volvo",
);

echo $cars['ben'];

Output

Benz

In this example, we will take a PHP array of strings, and access the second element value using its key (ben).

Get multidimensional array item value

A multidimensional array is an array in an array.

$cars = array(
  array("Ferrari","Benz","BMW","Volvo"),
  array("Toyota","Audi")
);

echo $cars[0][1];

In this example, we will take a multidimensional array of strings, and access the second element of the first array using its index.

If your array is a multidimensional associative array, you can access the element value by using the key.

$cars = array(
  array("fer" => "Ferrari","ben"=>"Benz","bmw"=>"BMW","vol"=>"Volvo"),
  array("toy"=>"Toyota","aud"=>"Audi")
);

echo $cars[0]['ben'];

Output

Benz

Get the first elements of an array in PHP

It’s easy to get the first element of a simple numerical array. Just get the element with index 0.

$cars = array("Ferrari","Benz","BMW","Volvo");

echo $cars[0];

Output

Ferrari

This method doesn’t work for an associative array.

One of the most efficient ways of getting the first element of a numerical or associative array in PHP is to use the reset() function.

This function sets the internal pointer of the array to its first element and returns the value of the first element.

$cars = array("Ferrari","Benz","BMW","Volvo");

echo reset($cars);

Output

Ferrari

Note that some array doesn’t start from index 0, in this method you did not need to know the key for the first element to get its value.

Using reset() will change the internal pointer of the array. If you want to get the first element from the array without making any changes to it, you can use the array_key_first() function.

$cars = array("Ferrari","Benz","BMW","Volvo");

echo $cars[array_key_first($cars)];

Output

Ferrari

For this method it doesn’t matter if your array is Indexed or Associative, you can use it to get value of an Associative array.

$cars = ['fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW', 'vol' => 'Volvo'];

echo $cars[array_key_first($cars)];
echo "<br/>";
echo reset($cars);

Output

Ferrari
Ferrari

Multidimensional arrays are made from Indexed and Associative arrays, then you can use this method to access elements of the array in PHP.

$cars = array(
  array("fer" => "Ferrari","ben"=>"Benz","bmw"=>"BMW","vol"=>"Volvo"),
  array("toy"=>"Toyota","aud"=>"Audi")
);

echo $cars[0][array_key_first($cars[0])];
echo "<br/>";
echo reset($cars[0]);

Output

Ferrari
Ferrari

Get the last elements of an array in PHP

For the Indexed array, you can get the last element by calculating its index from the array length.

$cars = array("Ferrari","Benz","BMW","Volvo");

echo $cars[count($cars)-1];

Output

Volvo

Just remember that Indexed arrays start from zero and the count() function returns the length of the array. To get the last index of the array you have to subtract it from 1.

However, once again, this will not work with associative arrays.

You can use the end() function in PHP to get the last element of any PHP array. It will set the internal pointer to the last element of the array and return its value. This makes it similar to the reset() function

$cars = array("Ferrari","Benz","BMW","Volvo");

echo end($cars);

Output

Volvo

Just like the array_key_first(), there is also a corresponding array_key_last() function that gives you the last key of the array without modifying it.

$cars = array("Ferrari","Benz","BMW","Volvo");

echo $cars[array_key_last($cars)];

Output

Volvo

For this method it doesn’t matter if your array is Indexed or Associative, you can use it to get the value of an Associative array too.

$cars = ['fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW', 'vol' => 'Volvo'];

echo $cars[array_key_last($cars)];
echo "<br/>";
echo end($cars);

Output

Volvo
Volvo

Again, you can use these methods to access elements of the multidimensional array in PHP.

$cars = array(
  array("fer" => "Ferrari","ben"=>"Benz","bmw"=>"BMW","vol"=>"Volvo"),
  array("toy"=>"Toyota","aud"=>"Audi")
);

echo $cars[0][array_key_last($cars[0])];
echo "<br/>";
echo end($cars[0]);

Output

Volvo
Volvo

Using the PHP array_pop() function to access an array element

Using the array_pop() function, you can also extract the last value of the array, but with the difference that this function removes the last value from the array.

$cars = array("Ferrari", "Benz", "BMW", "Volvo");

var_dump(array_pop($cars));

var_dump($cars);

Output

Volvo

array (size=3)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  2 => string 'BMW' (length=3)

Using array_shift() function

Another function that you can use to extract the first value of the array is the array_shift() function. This function returns the value of the first item and removes the item from the array.

$cars = array(
    "fer" => "Ferrari",
    "ben" => "Benz",
    "bmw" => "BMW",
    "vol" => "Volvo"
);

$value = array_shift($cars);

var_dump($value);
var_dump($cars);

Output

Ferrari

array (size=3)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)
  'vol' => string 'Volvo' (length=5)

You may like