简体   繁体   中英

How to add items to multidimensional array in php?

I want to build an array of arrays which in the next step will be used as argument to json_encode() .

Each element in the array looks like this:

$element = array(
                 'ITEM_ID' => $itemID,
                 'STATUS' => $status
                )

An example of a desired result with two elements is:

array( array('ITEM_ID' => 1,'STATUS' => "ok"), array('ITEM_ID' => 2,'STATUS' => "not ok") )

I have tried:

array_push($elementArray, $element1);
array_push($elementArray, $element2); 

But is does not give the desired result. What should I do?

push_array is not a php functionyou can try with array_push() or more simple

Try with

$element = array(
    'ITEM_ID' => $itemID,
    'STATUS' => $status
)

$element2 = array(
    'ITEM_ID' => $itemID,
    'STATUS' => $status
)

$finalArray[] = $element;
$finalArray[] = $element2;

echo "<pre>";
print_r($finalArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM