PHP array_push() Function

PHP array_push() Function is used to push one or more elements into the end of array. The array_push() is a built-in function of PHP

We can add a string as well as numeric values into array.

Syntax :

array_push($array, $value1, $value2, $value3, $value4,............,$value_n);

Parameter,

$array : Required  It is type of array parameter.

$value : One value is Required. Others are Optional. Values are used  to insert into $array.

Return :  It returns array with new elements.

If the array has a key, value pair then the method will always add a numeric key to the pushed value.

Let's see below example to understand php array_push() Function in details.

Example :

<?php
echo "<br><br>Adding new elements in an array with no keys.<br>";
$tutorials= array("PHP", "HTML", "JAVASCRIPT");
array_push($tutorials, "JAVA", "CSS");
print_r($tutorials);

echo "<br><br>Adding new elements in an array with key, value pair. Index key will be started from 4 then 5<br>";
$tutorials= array(1=>"PHP", 2=>"HTML", 3=>"JAVASCRIPT");
array_push($tutorials, "JAVA", "CSS");
print_r($tutorials);

echo "<br><br>Adding new elements in an array with key, value pair where key is string. Index key will be started from 0 then 1<br>";
$fruits= array("a"=>"Apple", "b"=>"Banana", "Grapes"=>"JAVASCRIPT");
array_push($fruits, "Mango", "Orange");
print_r($fruits);

?>

Comments

Leave a Reply

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

17195