php array add element

PHP Array Add Element

Adding an element to the PHP array can be done in some methods. In this article, we are going to discuss the PHP array to add an element to the start and end, how to add an element to an array with a numeric or other key and how to insert an item to a PHP array in a loop.

PHP Array Add Element Content

Add an element to the Indexed array

This method shows how to add an element to an array. This is the easiest way to do that.

$cars = array('Ferrari', 'Benz', 'BMW');

$cars[] = 'Volvo';

var_dump($cars);

Output

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

This method adds an item to the end of the PHP array.

Adds an element to an associative array

Add an element to an associative array in PHP with a key:

$cars = array('fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW');

$cars['vol'] = 'Volvo';

var_dump($cars);

Output

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

This example shows how to add an element to an array with a key at the end of the array.

Add an item to the end of the PHP array

The two above methods do the job, but there is a built-in function to add an element to the end of the array like the array_push() function. This function doesn’t allow you to add the element with the key. This method only works for indexed arrays.

$cars = array('fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW');

array_push($cars, 'Volvo');

var_dump($cars);

Output

array (size=4)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)
  0 => string 'Volvo' (length=5)

Read More About: PHP array_push() function

Add an element to start in the array

To add an element to the beginning of the array, you can use the array_unshift() function.

$cars = array('fer' => 'Ferrari', 'ben' => 'Benz', 'bmw' => 'BMW');

array_unshift($cars, 'Volvo');

var_dump($cars);

Output

array (size=4)
  0 => string 'Volvo' (length=5)
  'fer' => string 'Ferrari' (length=7)
  'ben' => string 'Benz' (length=4)
  'bmw' => string 'BMW' (length=3)

Like the array_push() function, you can’t add an element to an associative array with a key.

Add an element with a numeric key

An array with a numeric key is known as an Index array and we discussed this above. However, this example displays how to do that.

$cars = array(0 => 'Ferrari', 2 => 'Benz', 3 => 'BMW');

$cars[1] = 'Volvo';

var_dump($cars);

Output

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

The element with a numeric key was added but it is not in the right order. To order the array you have to sort the array with a key.

More About PHP Array Sort

Add an element in a loop

To add an element in the loop you can use the methods above to add an element.

$numbers = array();
for ($counter = 0; $counter < 3; $counter++) {
    $numbers[] = $counter;
    array_push($numbers, $counter);
}

var_dump($numbers);

Output

array (size=6)
  0 => int 0
  1 => int 0
  2 => int 1
  3 => int 1
  4 => int 2
  5 => int 2

In this example, inside the loop, we added an element to the end of the array with two methods.

Add an element to a multidimensional array in PHP

Adding an element to the multidimensional array could be a little confusing. You have to know exactly which level you want to add the element. This example adds an element to the first level.

$cars = array(
    array('fer' => 'Ferrari'),
    array('ben' => 'Benz'),
    array('bmw' => 'BMW')
);

$cars[] = array('vol' => 'Volvo');

var_dump($cars);

Output

array (size=4)
  0 => 
    array (size=1)
      'fer' => string 'Ferrari' (length=7)
  1 => 
    array (size=1)
      'ben' => string 'Benz' (length=4)
  2 => 
    array (size=1)
      'bmw' => string 'BMW' (length=3)
  3 => 
    array (size=1)
      'vol' => string 'Volvo' (length=5)

This example adds to the second level.

$cars = array(
    array('fer' => 'Ferrari'),
    array('ben' => 'Benz'),
    array('bmw' => 'BMW')
);

$cars[2]['vol'] = 'Volvo';

var_dump($cars);

Output

array (size=3)
  0 => 
    array (size=1)
      'fer' => string 'Ferrari' (length=7)
  1 => 
    array (size=1)
      'ben' => string 'Benz' (length=4)
  2 => 
    array (size=2)
      'bmw' => string 'BMW' (length=3)
      'vol' => string 'Volvo' (length=5)

You may like