Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将值插入多维数组,但只插入了1个值_Php_Arrays_Multidimensional Array - Fatal编程技术网

Php 将值插入多维数组,但只插入了1个值

Php 将值插入多维数组,但只插入了1个值,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有这个阵列: $order_list = array ( array ("tangible", 1, 8, 1, 19000), array ("tangible", 6, 2, 10, NULL), array ("tangible", 1, 17, 1, 28000)); 我想把它作为一个输出: Array ( [1] => Array //$order_list[1]

我有这个阵列:

$order_list = array ( array ("tangible", 1, 8, 1, 19000),
                      array ("tangible", 6, 2, 10, NULL),
                      array ("tangible", 1, 17, 1, 28000));
我想把它作为一个输出:

Array
(
    [1] => Array       //$order_list[1]
        (
            [0] => 8   //$order_list[2]
            [1] => 17  //$order_list[2]
        )

    [6] => Array       //$order_list[1]
        (
            [0] => 2   //$order_list[2]
        )

)
这是我的密码:

$order_array = array ();

foreach ($order_list as $value) {

    $vendor_id = $value[1];
    $product_id = array($value[2]);
    $order_array[$vendor_id] = $product_id;

}

echo '<pre>';
print_r($order_array);
如何做到这一点:

foreach ($order_list as $value) {

    $vendor_id = $value[1];
    $product_id = $value[2]; // just that single element, no need to assign it into another container
    $order_array[$vendor_id][] = $product_id;
    // use as key   ^        ^ then just push it

}

非常感谢您的帮助。

无需将其他值转换为另一个单独的数组。正常情况下只需按一下,一个用作键(在本例中为
$vendor\u id
),然后另一个用作另一个正常值(在本例中为
$product\u id
,而不是
数组($value[2])
):

通过这样做:


这将覆盖该密钥对值,而不是不断地将元素推入其中。

@RobertHanson很高兴这有帮助
    [1] => Array
        (
            [0] => 8
            [1] => 17 //second value inserted into same array
        )
foreach ($order_list as $value) {

    $vendor_id = $value[1];
    $product_id = $value[2]; // just that single element, no need to assign it into another container
    $order_array[$vendor_id][] = $product_id;
    // use as key   ^        ^ then just push it

}
$order_array[$vendor_id] = $product_id;