PHP array push function utilization in proper way

PHP array push function utilization in proper way
By ID Media | Friday 1st July 2016

There are a lot of PHP awesome functions that have good approach to modern programming.  In overview around PHP server side scripting language the functions play vital role to be dynamic. PHP its self power full language but it’s awesome functions make it more powerful and meaning full.

One of the very famous PHP function is called array push. Its magic on array elements and add new element inside the PHP array.

Lets have a close look with example

<?php
$array_one=array(“White”,”Black”,”Red”);
array_push($array_one,”Yellow”,”Cyn”);
print_r($array_one);
?>

The result of the above snippet you will get following

Array ( [0] => White [1] => Black [2] => Red [3] => Yellow [4] => Cyn )

 

Lets have another example:

 

<?php
$array_one=array(‘key1’=>’value1′,’key2’=>’value2′,’key3’=>’value3′,’key4’=>’value4’);
$array_two=array(‘key1’=>’value1′,’key5’=>’value5’);
array_push($array_one,$array_two);
print_r($array_one);
?>

The result of the above snippet you will get following

Array ( [key1] => value1 [key2] => value2 [key3] => value3 [key4] => value4 [0] => Array ( [key1] => value1 [key5] => value5 ) )

Off course you may find the documentation on PHP official website here