Answers for "how to push values in array in php"

PHP
67

php append to array

$myArr = [1, 2, 3, 4];

array_push($myArr, 5, 8);
print_r($myArr); // [1, 2, 3, 4, 5, 8]

$myArr[] = -1;
print_r($myArr); // [1, 2, 3, 4, 5, 8, -1]
Posted by: Guest on January-21-2020
3

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 )
Posted by: Guest on December-29-2020
6

add item to array in php

<?php

$a=array("red","green");

array_push($a,"blue","yellow");

print_r($a);
?>
Posted by: Guest on May-25-2020
2

php add to array

$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>";
Posted by: Guest on April-25-2020
3

php add variable to array

<?php
	$myArray = array(); // Init empty array

	$myArray[] = $value; // Add value to the array
?>
Posted by: Guest on February-03-2021

Browse Popular Code Answers by Language