Home » Blogs » PHP array push function utilization in proper way.

PHP array push function utilization in proper way.

PHP array is very basic and essential function of php programming language. It provides php to be more dynamic by storing many values in a single variable.

By InDesign Media | Friday 1st July 2016
Total 701 Views
php array

In computer science, an array data structure, or simply an array, is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple by a mathematical formula. The simplest type of data structure is a linear array, also called one-dimensional array.

PHP array is the best way to store data on the fly. There are a lot of PHP awesome functions that have a good approach to modern programming.  Additionally, around PHP server-side scripting language, the functions play a vital role to be dynamic. Not to mention, PHP is self-power full language but it’s awesome functions make it more powerful and meaning full. Thus, life without it for any PHP frameworks is impossible. Every PHP frameworks use it.

Additionally, one of the very famous PHP function is called array push. It’s magic on array elements and adds new element inside the PHP array.

Lets have a close look with example

$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 )

Also, lets have another example:

$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);
?>

Thus, 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 hereI

Thank you for your interest on . Please fill out the form below and send it to us.