What does array push do in php?

What does array push do in php?
The PHP array_push is a function that helps in appending values in an already existing array. Certainly, the given function adds more values without disturbing the order of the existing elements. So, in this post, you’ll learn about the working of the PHP array_push function along with its alternatives.

Continue reading to add elements in an array similar to the way you place new books at the top of your book pile.

Contents

  • What Is PHP Array Push?
    • – Syntax Details
    • – Note:
  • PHP Append to Array: Learn Through Basic Example
    • – Coding Example
  • PHP Add to Associative Array: Appending Element Values
    • – Coding Example
  • PHP Add to Associative Array: Appending Key-value Pairs:
    • – Coding Example
  • PHP Array Push Vs “Array[] =”: Comparison
    • – Coding Example Using “Array[] =” Syntax:
  • Adding an Array as Value
    • – Coding Example
  • Final Thoughts

What Is PHP Array Push?

The PHP array_push is a function that accepts an array and one or more values that you want to append in the array Moreover, it doesn’t allow specifying keys but only values. Hence, it is clear that the given function stores the newly appended array values on numeric keys. Next, it returns the number of elements present in the array.

Also, the stated behavior of the given function directs to the idea that you can’t append elements with named keys in an associative array by using the PHP array_push function. However, there is another way to deal with the process of appending elements with named keys that we will discuss below in the article.

For now, please have a look at the syntax of the array_push function:

array_push(array, value1, value2, …)

– Syntax Details

Referring to the above syntax, the three dots represent that you can add as many values as you want. Also, it would be good to note that the value parameters have been declared optional in PHP version 7.3. However, before PHP 7.3, at least one value was required along with the array. But now, no error or warning will be thrown if you don’t pass any value to the PHP array_push function.

– Note:

A warning will be displayed if the first parameter of the PHP array_push function is not an array.

PHP Append to Array: Learn Through Basic Example

Undeniably, real-time examples are the backbone of all explanations. So, it would be great to discuss an example here as well.

Imagine that you have an array of flowers and now, you want to add more flower names to it. All you have to do is to implement the PHP array_push function by passing the “flowers” array and new flower names to it. Next, you can display the updated array by using the print_r() function.

– Coding Example

Here is an example code snippet to help out with array_push function implementation:

<?php
// creating an array of flowers
$flowers = array(“Tulip”,”Rose”,”Jasmine”);
// using the array_push() function
$res = array_push($flowers,”Sunflower”,”Aster”);
// printing the result returned by the function
echo “$res <br>”;
// printing the updated array
print_r($flowers);
?>

The following output will be displayed on the browser:

5
Array ( [0] => Tulip [1] => Rose [2] => Jasmine [3] => Sunflower [4] => Aster )

PHP Add to Associative Array: Appending Element Values

Certainly, an associative array has named keys but it isn’t important to have all the elements with named keys. So, you can append only values in an existing associative array by using the PHP array_push function without considering their keys. Consequently, the newly-added values will have index numbers starting from “0” following the existing elements with named keys.

– Coding Example

For example, let’s say you have an associative array and now, you want to append more values to it. Although its existing elements have named keys, you aren’t concerned about the keys of the values that you want to add now. So, here you’ll use the PHP array_push function to achieve your desired results.

Please have a look at the code snippet given below for further assistance:

<?php
// creating an array of course details
$course = array(
“name” => “Web Development”,
“duration” => “Three Months”,
“timing” => “11:00 AM to 1:00 PM”
);
// using the array_push() function
$res = array_push($course,”Monday to Friday”, “Online”);
// printing the result returned by the function
echo “$res <br>”;
// printing the updated array
print_r($course);
?>

Here is the output that you’ll get:

5
Array ( [name] => Web Development [duration] => Three Months [timing] => 11:00 AM to 1:00 PM [0] => Monday to Friday [1] => Online )

PHP Add to Associative Array: Appending Key-value Pairs:

Unfortunately, the PHP array_push function doesn’t allow adding key-value pairs. Therefore, you’ll need to opt for another way to get your work done. But don’t worry, it isn’t difficult. All you have to do is to write the name of an existing array, add the “+=” arithmetic assignment operator, and specify the key-value pairs separated by commas in square brackets.

The above process will ensure that the newly-added key-value pairs are added at the end of the array and the order of the array isn’t disturbed.

Please refer to the given syntax for this:

array  += [key => value, key => value, …]

– Coding Example

Here is another example: you have an associative array in which you want to append more key-value pairs. So, as per the above syntax, you’ll simply need to specify the key-value pairs and add them to the array using the “+=” operator.

Please feel free to follow the code block below to see how the example works:

<?php
// creating an array of student details
$std = array(
“std01” => 500,
“std02” => 470,
“std03” => 450
);
// adding a key-value pair
$std += [‘std04’ => 390];
// adding multiple key-value pairs at once
$std += [‘std05’ => 430, “std06” => 420];
// printing the updated array of students
print_r($std);
?>

This is the output that you’ll get:

Array ( [std01] => 500 [std02] => 470 [std03] => 450 [std04] => 390 [std05] => 430 [std06] => 420 )

PHP Array Push Vs “Array[] =”: Comparison

Undoubtedly, the “array[] =syntax also allows adding more values to an array, and the given statement questions the existence of the PHP array_push function.

Let’s analyze this comparison: the PHP array_push function throws a warning when its first parameter is not an array. Hence, the array must already exist before passing it to the array_push function. On the other hand, the “array[] =” syntax ends up creating another array if the specified array doesn’t already exist.

Now, if you are sure about the existence of the array and you want to add only a single element, then opting for the “array[] =” syntax is not a bad idea. However, if you want to add multiple values simultaneously then the PHP array_push function is the ultimate choice.

– Coding Example Using “Array[] =” Syntax:

For example, let’s assume you want to add only a single element to an existing array. Here, you’ll use the “array[] =” and get your work done without making a function call.

Please refer to the below code snippet for PHP adding to array:

<?php
// creating an array of languages
$arr1 = array(“PHP”,”HTML”,”CSS”);
// using the array[] = syntax
$arr1[] = “JavaScript”;
// printing the updated array
print_r($arr1);
?>

The above code will provide the following output:

Array ( [0] => PHP [1] => HTML [2] => CSS [3] => JavaScript )

Adding an Array as Value

What happens when you pass two arrays as arguments to the PHP array_push function? Certainly, the array that you’ll pass as a value will be appended to the first array resulting in a multidimensional array. However, preceding the second array with the “…” scatter operator will let you append the array values at the end of the first array.

– Coding Example

Here is a code snippet that adds an array to another array using the PHP array_push function:

<?php
// creating an array
$colors = array(“Red”,”Green”,”Orange”);
// creating another array
$selected = array(“Blue”,”Black”);
// using the array_push function to append array values
array_push($colors, …$selected);
// using the array_push function to create a multidimensional array
array_push($colors, $selected);
// printing the updated array
print_r($colors);
?>

Here is the output:

Array ( [0] => Red [1] => Green [2] => Orange [3] => Blue [4] => Black [5] => Array ( [0] => Blue [1] => Black ) )

Final Thoughts

Compiling the above discussion, you learned that the PHP array_push function allows efficiently appending a long list of values in an existing array simultaneously. Now, please have a look at some important points to strengthen your concept regarding the results generated after using the PHP array push function:

  • The PHP array_push function helps in appending elements at the end of an array
  • The PHP array push accepts an array and one or more values as arguments while it returns the length of the updated array
  • You can pass an array as a value to the PHP array_push function
  • The array push function stores the newly-appended values on numeric keys
  • The array_push doesn’t allow specifying keys for the values to be appended
  • You can use an alternative method comprising the “+=” operator to append key-values pairs to an array

What does array push do in php?
So, regardless of the current length and order of the array, you can safely add elements to array PHP. Moreover, through our guide, it has become easier for you to create a multidimensional array by forming individual arrays and pushing them into another array.

  • Author
  • Recent Posts

What does array push do in php?

Position Is Everything: Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL.

What does array push do in php?

What is difference between array push and pop in PHP?

Array push is used to add value in the array and Array pop is used to remove value from the array.

Can you push an array into an array PHP?

The array_push() method takes a single element or an array of elements and appends it to the array. You may add as many values as you need. Your inserted elements will always have numeric keys, even if the array itself has string keys. PHP array push() function has been introduced in PHP 4.

What is push and pop in PHP?

In PHP, the array methods like array_push and array_pop is used to add or remove the elements or items from the array. In PHP, The array_push method can use to add or insert one or more items or elements from the end of an array.

How do you push items into an array?

JavaScript Array push() The push() method adds new items to the end of an array. The push() method changes the length of the array. The push() method returns the new length.