0

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>";
 }

}

4
  • Please post your full code. Commented Oct 26, 2015 at 14:42
  • Possible duplicate of Insert values to php multidimensional array Commented Oct 26, 2015 at 14:42
  • I think the problem here is that you set $itemsId=[] just before the push, can you please show us your full code? Commented Oct 26, 2015 at 14:42
  • As i said the problem is that you always set to empty $itemsID with the portion of code $itemsID = []; delete it and initialize it at before the loop Commented Oct 26, 2015 at 14:48

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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>";
         }
    }

Comments

0

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>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.