array insertion in php code example
Example 1: php add to array
$fruits = ["apple", "banana"];
// array_push() function inserts one or more elements to the end of an array
array_push($fruits, "orange");
// If you use array_push() to add one element to the array, it's better to use
// $fruits[] = because in that way there is no overhead of calling a function.
$fruits[] = "orange";
// output: Array ( [0] => apple [1] => banana [2] => orange )
Example 2: php insert in array
$original = array( 'a', 'b', 'c', 'd', 'e' );
$inserted = array( 'x' ); // not necessarily an array, see manual quote
array_splice( $original, 3, 0, $inserted ); // splice in at position 3
// $original is now a b c x d e
Example 3: array push in php
$cart = array();
$cart[] = 13;
$cart[] = 14;
// etc
//Above is correct. but below one is for further understanding
$cart = array();
for($i=0;$i<=5;$i++){
$cart[] = $i;
}
echo "<pre>";
print_r($cart);
echo "</pre>";