在数组末端正确插入数组

时间:2015-10-26 14:39:16

标签: php arrays insert

我有一个这样的数组:

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

我想创建另一个包含不同$itemID的数组,因为$value会不断变化。

我试图像这样使用array_push

 $itemsID = [ ]; 

    array_push($itemsID, $itemID);

输出是:

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

 )

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

 )

我希望输出为:

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

我怎样才能做到这一点?

完整代码:

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

}

3 个答案:

答案 0 :(得分:2)

您每次迭代都会重新创建$itemsID。而是在循环外创建一次,在llop中添加它,并在循环后检查它:

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

注意我将您的变量重命名为$item$itemList以更好地描述其用法 - 模糊的变量名称使代码难以理解

答案 1 :(得分: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);

未经测试,但您明白了。 编辑到您的代码:

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

答案 2 :(得分:0)

使用此行$itemsID = [ ];,您将向主阵列添加另一级别的数组。如果没有该行,代码将按您的意愿执行。

另外,为数组添加新出现的更简单的语法只是执行$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>";