Aman
Aman

Reputation: 155

Insert array inside a array at the end properly

I have an array like this:

$itemID = [ 
    "appid" => 570,
    "contextid" => "2",
    "amount" => 1,
    "assetid" => $value 
];

I want to make another array which contains different $itemIDs because $value will keep changing.

I'm trying to use array_push like this:

 $itemsID = [ ]; 

    array_push($itemsID, $itemID);

the output is:

Array
(
[0] => Array
    (
        [appid] => 570
        [contextid] => 2
        [amount] => 1
        [assetid] => 5628263595
    )

 )

Array
(
[0] => Array
    (
        [appid] => 570
        [contextid] => 2
        [amount] => 1
        [assetid] => 3651140937
    )

 )

I want the output to be:

Array
(
[0] => Array
    (
        [appid] => 570
        [contextid] => 2
        [amount] => 1
        [assetid] => 5628263595
[1] => Array
    (
        [appid] => 570
        [contextid] => 2
        [amount] => 1
        [assetid] => 3651140937
    )

how can I achieve this?

full code:

foreach ($select as $key => $value) {
 if(array_key_exists($key, $select)) {

    $itemID = [ "appid" => 570,
    "contextid" => "2",
    "amount" => 1,
    "assetid" => $value ];

    $itemsID = [ ]; 

    array_push($itemsID, $itemID);

    echo "<pre>";
    print_r($itemsID);
    echo "</pre>";
 }

}

Upvotes: 0

Views: 57

Answers (3)

RiggsFolly
RiggsFolly

Reputation: 94662

With this line $itemsID = [ ]; you are adding another level of arrays to your main array. Without that line the code will do as you want.

Also a simpler syntax to add a new occurance to an array is just simply to do $itemsID[] = $itemID;

$itemsID = []; // create the array to hold result

foreach ($select as $key => $value) {
    if(array_key_exists($key, $select)) {

        $itemID = [ "appid" => 570,
                    "contextid" => "2",
                    "amount" => 1,
                    "assetid" => $value ];

        //  add another occurance to the array    
        // containing the new $itemID array
        $itemsID[] = $itemID; 
    }
}
// print the results
echo "<pre>";
print_r($itemsID);
echo "</pre>";

Upvotes: 0

Steve
Steve

Reputation: 20469

You are recreating $itemsID every iteration. Instead create it once outside the loop, add to it within the llop, and check it after the loop:

$itemList = [ ];
foreach ($select as $key => $value) {
    if(array_key_exists($key, $select)) {

        $item = [ "appid" => 570,
                    "contextid" => "2",
                    "amount" => 1,
                    "assetid" => $value ];
        array_push($itemList, $item);
    }
}
echo "<pre>";
print_r($itemList);
echo "</pre>";

Note i renamed your variables to $item and $itemList to better describe their usage - ambiguous variable names make code hard to follow

Upvotes: 2

Rafał Cz.
Rafał Cz.

Reputation: 787

http://php.net/manual/en/function.array-merge.php

$itemID = [ "appid" => 570,
    "contextid" => "2",
    "amount" => 1,
    "assetid" => $value ];

$itemID2 = [ "appid" => 570,
    "contextid" => "2",
    "amount" => 1,
    "assetid" => $value ];

$result = array_merge($itemID, $itemID2);
print_r($result);

Did not tested, but you get the idea. Edited to your code:

    $itemsID = array();
    foreach ($select as $key => $value) {
         if(array_key_exists($key, $select)) {

            $itemID = [ "appid" => 570,
            "contextid" => "2",
            "amount" => 1,
            "assetid" => $value ];

            $itemsID[] = array_merge($itemsID, $itemID);

            echo "<pre>";
            print_r($itemsID);
            echo "</pre>";
         }
    }

Upvotes: 0

Related Questions