How to add elements to an array in PHP

Add values or elements to an array in PHP; Through this tutorial, i am going to show you how to add/push the values/elements to array in PHP.

How to add elements to an array in PHP

Using the PHP array_push() function, you can add or insert one or more elements/values to the end of an array.

Now, i will take following examples to add or insert elements in array in php; is as follows:

  • Example 1 – add values in array PHP
  • Example 2 – PHP array push with key
  • Example 3 – PHP add to multidimensional array
  • Example 4 – How to push array in multidimensional array
  • Example 5 – PHP append one array to another | PHP push array into an array

Example 1 – add values in array PHP

Let’s take first example to add or insert values in php array; is as follows:

<?php
 $array = array("php", "laravel", "codeigniter");
 //before adding new values 
 echo "Before add the value:- ";
 print_r($array); echo "<br>";
 //add elements/values in array
 array_push($array,"wordpress","bootstrap","html");
 //after adding a new values
 echo "After add the value:- ";
 print_r($array);
 
?>

Example 2 – PHP array push with key

Let, you have one new array like this ” $array = array(“a”=>”red”,”b”=>”green”); “. And want to add the values into array with key. Let’s see the following example; is as follows:

// add the values in array without using array function
  $array['c'] = "yello";
  $array['d'] = "brown";

To push the values in array with key without using array function:

<?php
 $array = array("a"=>"red","b"=>"green");
 //before adding new values 
 echo "Before add the value:- ";
 print_r($array);
 echo "<br>";
 // add the values in array without using array function
 $array['c'] = "yello";
 $array['d'] = "brown";
 //after adding a new values
 echo "After add the value:- ";
 print_r($array);
?>

Example 3 – PHP add to multidimensional array

To add values/elements in a multi-dimensional array; You will take an example for adding the values/elements in a multidimensional array.

If you have a multidimensional array like this:

$array = [
     'web' => ['html', 'css', 'bootstrap'],
     'p_lang' => ['php', 'python', 'cabbage'],
     'framework' => ['laravel', 'codeigniter']
 ];

And you want to add values/elements inside the array elements. You can use the below example for adding the values/elements in the multidimensional array:

<?php
 $array = [
     'web' => ['html', 'css', 'bootstrap'],
     'p_lang' => ['php', 'python', 'cabbage'],
     'framework' => ['laravel', 'codeigniter']
 ];
 //before adding new values 
 echo "Before add the value:- ";
 print_r($array); echo "<br>";
 //add elements/values in array
 array_push($array['framework'], 'wordpress', 'joomla');
 print_r($array);
 //after adding a new values
 echo "After add the value:- ";
 print_r($array);
 
?>

Example 4 – How to push array in multidimensional array

Add array elements in multidimensional array in php; is as follows:

<?php
 $array = [
     'web' => ['html', 'css', 'bootstrap'],
     'p_lang' => ['php', 'python', 'cabbage'],
     'framework' => ['laravel', 'codeigniter']
 ];
 
 $array1['test'] = array('laravel', 'codeigniter');
 //before adding new values 
 echo "Before add the value:- ";
 print_r($array); echo "<br>";
 //add elements/values in array
 array_push($array['framework'], $array1);
 print_r($array);
 //after adding a new values
 echo "After add the value:- ";
 print_r($array);
 
?>

Example 5 – PHP append one array to another | PHP push array into an array

To push one array to another array or push array into an array without using array_push() function; is as follows:

<?php
 $a = array('a', 'b');
 $b = array('c', 'd');
 $merge = array_merge($a, $b); 
?> 

Conclusion

Array add/push values PHP; Through this tutorial, you have learned how to add values in array PHP, PHP array push with key, PHP add to an associative array, PHP add to the multidimensional array, array push associative array PHP, PHP array adds key-value pair to an existing array with examples.

Recommended PHP Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.