Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pushing objects into existing array with php

Tags:

arrays

php

In JS I would do something like this:

var arr = [];  
arr.push({ 
   sku : foo, 
   quantity: bar 
});

How do I do this with PHP? When I try to do this I'm getting a parsing error.

For example with PHP:

$someArray = array(); // or maybe someArray = []; ???
$someArray = array_push(
  'sku' => $sku,
  'quantity' => $quantity
);

Is this correct?

Thank you!

like image 864
jremi Avatar asked Mar 15 '26 16:03

jremi


1 Answers

check this: array_push

you could do:

$array = [];
array_push($array, "item");

or

$array = [];
$item = "hi";
$array['key'] = $item;

or you could use

$array = [];
array_merge($array, ["abc" => 1]);

also your js code is equivilant to

$array = [];
array_push($array, ['item' => 'value', 'item1' => 'value1']);


// is equivalent to 
var arr = [];  
arr.push({ 
   sku : foo, 
   quantity: bar 
});
like image 196
HSLM Avatar answered Mar 18 '26 05:03

HSLM