Wednesday, 24 September 2014

array_push in PHP

array_push() function is utilized to insert one or more elements to the end of an input array.

The PHP array_push() function includes values onto the end of the array, returning true on success and False otherwise.
You can push multiple values into the array synchronously, by passing these values into the function as info parameters.

Syntax:

array_push(array,value1,value2…)

Parameters Description:

array : Required. Specifies an array
value1 : Required. Specifies the value to add
value2 : Optional. Specifies the value to add

Example:

<?php
$daysarr1 = array( "Sunday","Monday" );
array_push($daysarr1,"Tuesday","Wednesday");
print_r($daysarr1);
$daysarr2 = array("a"=>"Thursday","b"=>"Friday");
array_push($daysarr2,"Saturday","Sunday");
echo "<br />"."using string keys";
print_r($daysarr2);
?>

O/P:


Array (
         [0] => Sunday 
         [1] => Monday 
         [2] => Tuesday
         [3] => Wednesday
       )
using string keys
Array ( 
         [a] => Thursday 
         [b] => Friday
         [0] => Saturday
         [1] => Sunday 
       )

0 comments:

Post a Comment