Answers for "php push object to array"

PHP
4

array push object php

$myArray = [];

array_push($myArray, (object)[
        'key1' => 'someValue',
        'key2' => 'someValue2',
        'key3' => 'someValue3',
]);

return $myArray;
Posted by: Guest on March-05-2021
68

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
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
3

add object in array php

$myArray = array("name" => "my name");
echo json_encode($myArray);
Posted by: Guest on April-27-2020
0

push collection php

$collection = collect([1, 2, 3, 4]);

$collection->push(5);

$collection->all();

// [1, 2, 3, 4, 5]
Posted by: Guest on May-27-2020

Browse Popular Code Answers by Language