简体   繁体   English

如何从一维数组创建具有关联子数组的数组?

[英]How to create an array with associative subarrays from a one-dimensional array?

I have the following array. 我有以下数组。

Array(
[1041] => 30
[1046] => 10
[1047] => 10
)

and I want to store it like following. 我想像下面这样存储它。

Array([0] => Array
(
    [material_name] => 1041
    [material_qty] => 30
) 
[1] => Array
(
    [material_name] => 1046
    [material_qty] => 10
)
[2] => Array
(
    [material_name] => 1047
    [material_qty] => 10
) )

now i am trying to store key values but it stores only last one. 现在我试图存储键值,但它只存储最后一个。

for($i=0; $i<count($materials); $i++){
              foreach($materials as $key => $value){
                 $dataArrMaterial[$i] = array(
                        'material_name' => $key,
              'material_qty' => $value
                         );
              } }
              _print($dataArrMaterial);

my output is follwoing. 我的输出如下。

Array(
[0] => Array
    (
        [material_name] => 1047
        [material_qty] => 10
    )

[1] => Array
    (
        [material_name] => 1047
        [material_qty] => 10
    )

[2] => Array
    (
        [material_name] => 1047
        [material_qty] => 10
    ) )

now please help me do to that. 现在请帮助我做到这一点。 thanks in advance. 提前致谢。

You can simplify this quite a lot: 您可以将其简化很多:

<?php
// ...
$dataArrMaterial = [];
foreach ($materials as $materialName => $materialQty) {
    $dataArrMaterial[] = [
        "material_name" => $materialName ,
        "material_qty" => $materialQty
    ];
}

Since it wasn't explained to you why , I will. 由于没有向您解释原因 ,我会的。

for($i=0; $i<count($materials); $i++){
    foreach($materials as $key => $value){
        // the value of $i is not changed within this loop
        $dataArrMaterial[$i] = array('material_name' => $key, 'material_qty' => $value);
    }
    // when the for() loop starts again, $i is incremented
}

So, on the first iteration of the foreach loop, you are assigning all subarrays to the same $i key ( 0 ). 因此,在foreach循环的第一次迭代中,您正在将所有子数组分配给相同的$i键( 0 )。 After 1041 is looped, your output array looks like this: 1041循环后,您的输出数组如下所示:

[0 => ["material_name" => 1041, "material_qty" => 30]];

After 1046 , the first pushed subarray is overwritten because keys on the same level must be unique. 1046之后,第一个推送的子数组将被覆盖,因为同一级别上的键必须唯一。

[0 => ["material_name" => 1046, "material_qty" => 10]];

Then the third element overwrites the second in the output array: 然后,第三个元素将覆盖输出数组中的第二个元素:

[0 => ["material_name" => 1047, "material_qty" => 10]];

Once the inner loop ( foreach ) is finished, the outer loop ( for ) increments $i and the "overwriting" process repeats for the 1 key of the output array. 内部循环( foreach )完成后,外部循环( for )将增加$i并且对输出数组的1键重复执行“覆盖”过程。 And then again because count($materials) = 3 (three iterations). 然后再一次,因为count($materials) = 3 (三个迭代)。

This is why you are getting the final set of values repeated like this: 这就是为什么您要像这样重复获取最后一组值的原因:

[0 => ["material_name" => 1047, "material_qty" => 10]];
[1 => ["material_name" => 1047, "material_qty" => 10]];
[2 => ["material_name" => 1047, "material_qty" => 10]];

As for the best way to perform this task, you can use a solution from my duplicate link or do it as Arkascha has demonstrated. 至于执行此任务的最佳方法,您可以使用我重复的链接中的解决方案,也可以按照Arkascha的说明进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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