PHP array push: How to Add Elements to Array in PHP

PHP array push: How to Add Elements to Array in PHP

Add elements/items to an array in PHP; In this tutorial, we would like to share with you how to add or push the items/elements to array in PHP.

It has a vast array of built-in functions that make programming in PHP easy and efficient. One such function is array_push(). The array_push() function is used to add elements to the end of an array. This article covers the following topics related to using array_push() in PHP:

  • Using array_push() with keys in PHP
  • Adding elements to an associative array using array_push() in PHP
  • Adding elements to a multidimensional associative array in PHP
  • Adding elements to a multidimensional array in PHP
  • Pushing an array into a multidimensional array in PHP
  • Appending one array to another in PHP

How to Add Elements to Array in PHP

Using the array_push(), you can add elements to an array in PHP.

The array_push() function is used to add one or more elements to the end of an array.

The syntax of array_push() is as follows:

array_push(array, value1, value2, …)

The first parameter, array, is the name of the array to which you want to add elements. The second parameter and subsequent parameters are the values that you want to add to the array.

Example 1 of Add Elements to Array using array_push() in PHP

Let’s consider an example where you have an array of colors, and you want to add a few more colors to the array. Here is the code to do this:

<?php

$colors = array("red", "green", "blue");

// Add elements to the end of the array
array_push($colors, "yellow", "orange");

print_r($colors);

?>

The code creates an indexed array called $colors with three initial elements: “red”, “green”, and “blue”. It then uses the array_push() function to add two more elements, “yellow” and “orange”, to the end of the array.

Finally, the code uses the print_r() function to display the contents of the $colors array, including the newly added elements, in a human-readable format.

So, when the code is executed, it will output the following:

Array
(
    [0] => red
    [1] => green
    [2] => blue
    [3] => yellow
    [4] => orange
)

This shows that the two new elements (“yellow” and “orange”) have been successfully added to the end of the $colors array.

Example 2 of array push key value pair php using array_push

Sure, here’s an example of using array_push in PHP to add a key-value pair to an array:

// Define an empty array
$myArray = array();

// Add a key-value pair to the array using array_push
array_push($myArray, array("key" => "value"));

// Print the array to verify the key-value pair was added
print_r($myArray);

This PHP code is creating an empty array and then adding a key-value pair to it using the array_push function. Finally, it prints the array using the print_r function to confirm that the key-value pair was added successfully.

Here’s what each line of code does:

  • $myArray = array(); creates an empty array called $myArray.
  • array_push($myArray, array("key" => "value")); adds an element to the end of $myArray. The element is an array with a single key-value pair, where the key is "key" and the value is "value".
  • print_r($myArray); prints the contents of $myArray, including the newly added key-value pair. The print_r function is used to print the array in a human-readable format.

So, after running this code, the output would be something like:

Array
(
    [0] => Array
        (
            [key] => value
        )

)

This shows that $myArray now contains a single element, which is an array with a key-value pair of "key" => "value".

Example 3 of add element to multidimensional array using array_push

Sure, here’s an example of adding an element to a multidimensional array using array_push() function in PHP:

<?php
// Define a multidimensional array
$myArray = array(
    array("apple", "banana", "orange"),
    array("carrot", "pepper", "onion")
);

// Add a new element to the first inner array using array_push()
array_push($myArray[0], "grape");

// Display the updated multidimensional array
print_r($myArray);
?>

The above code defines a multidimensional array called $myArray, which contains two inner arrays, each containing three elements.

Then, the array_push() function is used to add a new element “grape” to the first inner array of $myArray. The array_push() function adds an element to the end of an array and returns the new number of elements in the array.

Finally, the updated $myArray is displayed using the print_r() function, which prints the entire contents of the array, including all the nested arrays and their elements.

So, after executing this code, the output will be:

Array
(
    [0] => Array
        (
            [0] => apple
            [1] => banana
            [2] => orange
            [3] => grape
        )

    [1] => Array
        (
            [0] => carrot
            [1] => pepper
            [2] => onion
        )

)

Example 4 of How to push array in multidimensional array using array_push

Sure, here’s an example of how to use the array_push() function in PHP to push an array into a multidimensional array:

// Initialize the multidimensional array
$multiArray = array(
    array('apple', 'banana', 'orange'),
    array('carrot', 'broccoli', 'pepper')
);

// Define the array to push into the multidimensional array
$newArray = array('grape', 'kiwi', 'pineapple');

// Use array_push() to add the new array to the end of the multidimensional array
array_push($multiArray, $newArray);

// Print the updated multidimensional array
print_r($multiArray);

The above given code initializes a multidimensional array, adds a new one-dimensional array to it using array_push(), and then prints the updated multidimensional array using print_r(). Here’s a breakdown of what each line of the code does:

  1. array() function is used to initialize the multidimensional array $multiArray with two nested arrays, each containing three string elements.
  2. The $newArray is defined as an array containing three string elements, which will be added to the multidimensional array.
  3. array_push() is a built-in PHP function used to add elements to the end of an array. Here, it’s used to add $newArray to $multiArray.
  4. The print_r() function is used to output the contents of the $multiArray array, which now includes the added $newArray at the end.

The output of this code will be:

Array
(
    [0] => Array
        (
            [0] => apple
            [1] => banana
            [2] => orange
        )

    [1] => Array
        (
            [0] => carrot
            [1] => broccoli
            [2] => pepper
        )

    [2] => Array
        (
            [0] => grape
            [1] => kiwi
            [2] => pineapple
        )

)

PHP append one array to another | PHP push array into an array

Now, you will take example of push one array to another array or push array into an array without using array_push() function.

Add one array into another array in PHP:

<?php

 $a = array('a', 'b');
 $b = array('c', 'd');
 $merge = array_merge($a, $b); 

?> 

Conclusion

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

  1. PHP Array: Indexed,Associative, Multidimensional
  2. To Remove Elements or Values from Array PHP
  3. PHP remove duplicates from multidimensional array
  4. Remove Duplicate Elements or Values from Array PHP
  5. How to Convert String to Array in PHP
  6. Array Push and POP in PHP | PHP Tutorial
  7. PHP Search Multidimensional Array [key and value and return key]
  8. PHP Array to String Conversion – PHP Implode

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *