Home >>PHP Array Functions >PHP array_push() Function

PHP array_push() Function

PHP array_push() Function

PHP array_push() Function in the PHP language is generally used in order to insert one or more elements to the end of an array. It will be very beneficial if the users or the programmers can add one value or they can add as many as they like.

Please note that even if the programmer's array has string keys then also their added elements will always have numeric keys. To get more clarification about this topic refer to the example that has been mentioned below.

 

Syntax:

array_push(array, value1, value2, ...)

Parameter Values

Parameter Description
array This is generally used to specify an array and it is required.
value1 This parameter generally specifies the value in order to add. It is optional but it becomes mandatory in the versions before 7.3.
value1 This parameter generally specifies the value that has to add. It is also optional.

Here, is an example of array_push() Function in PHP:

<html>
<body>
<?php
$arr=array("state"=>"delhi","state2"=>"bihar");
array_push($arr,"up","haryana");
print_r($arr);
?>
</body>
</html>
Output:
Array ( 
	[state] => delhi 
	[state2] => bihar 
	[0] => up 
	[1] => haryana 
	)

Example 2:

<html>
<body>
<?php
$arg=array("white","pink");
array_push($arg,"yellow","red");
print_r($arg);
?>
</body>
</html>
Output:
Array ( 
	[0] => white 
	[1] => pink 
	[2] => yellow 
	[3] => red 
	)

No Sidebar ads