【问题标题】:Reorganizing multidimentional php array to another multidimentional array where first elemnt can be used as array key将多维php数组重组为另一个多维数组,其中第一个元素可以用作数组键
【发布时间】:2022-01-03 03:05:20
【问题描述】:

以输出如下所示的方式转换以下数组的最佳方法是什么。

初始数组

array:2 [▼
  0 => array:4 [▼
    "group" => "1"
    4 => "19"
    6 => "27"
    8 => "160"
  ]
  1 => array:4 [▼
    "group" => "2"
    4 => "20"
    6 => "28"
    8 => "200"
  ]
]

想要的数组:

array:6 [▼
  0 => array:3 [▼
       "group" => "1"
       "es_variation_set_id" => "4" << this is key form the initial array
       "es_variation_id" => "19" << this is value form the initial array
 ]
  1 => array:3 [▼
       "group" => "1"
       "es_variation_set_id" => "6" << this is key form the initial array
       "es_variation_id" => "28" << this is value form the initial array
 ]
  2 => array:3 [▼
       "group" => "1"
       "es_variation_set_id" => "8" << this is key form the initial array
       "es_variation_id" => "160" << this is value form the initial array
 ]
  3 => array:3 [▼
       "group" => "2"
       "es_variation_set_id" => "4" << this is key form the initial array
       "es_variation_id" => "20" << this is value form the initial array
 ]
  4 => array:3 [▼
       "group" => "2"
       "es_variation_set_id" => "6" << this is key form the initial array
       "es_variation_id" => "28" << this is value form the initial array
 ]
  5 => array:3 [▼
       "group" => "1"
       "es_variation_set_id" => "8" << this is key form the initial array
       "es_variation_id" => "200" << this is value form the initial array
 ]       
]

这是我的foreach

    foreach ($request->only('product_variations')['product_variations'] as $variation_value)
    {

        if($variation_value['group'] != 0){
            dd($variation_value);
        }
    }

请提出解决此问题的最佳方法

提前致谢

【问题讨论】:

    标签: php arrays laravel-7


    【解决方案1】:

    您可以使用嵌套的foreach 循环来解决这个问题,该循环在每次运行之前提取group 值。

    $output = [];
    foreach ($input as $subArray) {
        $group = $subArray['group'];
        unset($subArray['group']);
        foreach ($subArray as $setId => $variationId) {
            $output[] = [
                'group' => $group,
                'es_variation_set_id' => $setId,
                'es_variation_id' => $variationId,
            ];
        }
    }
    

    在这里演示:https://3v4l.org/KLf8Y

    【讨论】:

    • 谢谢你,克里斯,你的解决方案很完美,这是我要采用的解决方案,因为它似乎更适合我的其他目标
    【解决方案2】:

    你快到了。您只需要在 if 条件中添加另一个 foreach 即可单独添加组、键和值对。

    <?php
    
    $res = [];
    
    foreach ($request->only('product_variations')['product_variations'] as $variation_value){
        if($variation_value['group'] != 0){
            foreach($variation_value as $k => $v){
                if($k == 'group') continue;
                $res[] = [
                    'group' => $variation_value['group'],
                    'es_variation_set_id' => $k,
                    'es_variation_id' => $v
                ];
            }
        }
    }
    
    print_r($res);
    

    【讨论】:

    • 非常感谢您的快速响应,您的解决方案完美运行
    猜你喜欢
    • 1970-01-01
    • 2012-10-18
    • 2018-02-01
    • 2019-12-03
    • 2011-07-09
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多