Arrays can be great in manipulating data and PHP has different implementations that you can take advantage. In this series we are going to explore the different types of array functions that will be handy when you are trying to solve complex problems without having to 'loop' or iterate through these arrays.
The first function we are going to explore is the array_push() method. Basically this function "pushes" elements inside an array.
Array - Specifies an array that you are going to push data with.
Value1 - The value you wish to add.
Value2- The second value you wish to add.
<?php $bag = array("Apples","Mangoes","Lemons"); array_push($bag,"Cucumber","Onions"); echo var_dump($bag); ?>
The code above will automatically insert the vegetables "Cucumber" and "Onions" on the last part of the array.
Index 0: Apples
Index 1: Mangoes
Index 2: Lemons
Index 3: Cucumber
Index 4: Onions
If you noticed, that the Cucumber and Onions where added to the last part. Think of it as pushing something to the array. You can also add arrays to the list.
<?php $bag = array("Apples","Mangoes","Lemons"); $bag2 = array( "Vegetables"=>array("Onions","Squash","Peppers")); array_push($bag,$bag2); echo var_dump($bag); ?>
The following code will place $bag2 in which it is an array to be assigned in index of the array bag.