dongya1228 2018-02-22 14:37
浏览 25
已采纳

父组下的PHP组数组子项?

I have an array that returns the following:

   $products = [
    'product' => [
      [
        "product_parent" =>  "Parent Name 1",
        "product_name" =>  "Product 1",
      ],
      [
        "product_parent" =>  "Parent Name 1",
        "product_name" =>  "Product 2",
      ],
      [
        "product_parent" =>  "Parent Name 2",
        "product_name" =>  "Product 1",
      ],
      [
        "product_parent" =>  "Parent Name 3",
        "product_name" =>  "Product 1",
      ]
    ]
  ];

I need to return a new array that is structured like this that is an (indexed) array of (associative) arrays

   'product' => [
      0 => [
        'product_name' => 'Parent Name 1'
        'product_info' => [
          0 => [
            "product_name" =>  "Product 1",
          ],
          1 => [
            "product_name" =>  "Product 2",
          ]
        ]
      ],
      1 => [
        'product_name' => 'Parent Name 2'
        'product_info' => [
          0 => [
            "product_name" =>  "Product 1",
          ],
          1 => [
            "product_name" =>  "Product 2",
          ]
        ]
      ]
    ]

I've tried various foreach loops but I can't figure out a way to get the index set properly like the desired output I have above.

What I've Tried:

I also was originally working on this method, which is more efficient as it does less queries and improves performance. Hopefully this helps someone in the future.

  $variables = [];

  foreach($query_results as $key => $group) {
    $sub_groups = $this->db->select('*')
    ->from('products')
    ->where('parent_id', $group['id'])
    ->get();

    $variables[0]['product'][$key]['product_group'] = $group['product_name'];
    $variables[0]['product'][$key]['product_info']  = array_values($sub_groups->result_array());
  }

This will return the type of array structure I desired above, but with better performance.

  • 写回答

1条回答 默认 最新

  • douzi4724 2018-02-22 14:50
    关注

    You can use array_reduce to achieve this.

    $products = array(
        'product' => array(
          array(
            "product_parent" =>  "Parent Name 1",
            "product_name" =>  "Product 1",
          ),
          array(
            "product_parent" =>  "Parent Name 1",
            "product_name" =>  "Product 2",
          ),
          array(
            "product_parent" =>  "Parent Name 2",
            "product_name" =>  "Product 1",
          ),
          array(
            "product_parent" =>  "Parent Name 3",
            "product_name" =>  "Product 1",
          )
        )
    );
    
    //
    $result = array_reduce($products['product'], function($c, $v){
        if ( !isset( $c[ $v['product_parent'] ] ) ) $c[ $v['product_parent'] ] = array( 'product_name' => $v[ "product_parent" ], 'product_info' => array() );
        $c[ $v['product_parent'] ]["product_info"][] = array( "product_name" => $v[ "product_name" ] );     
        return $c;
    }, array());
    
    //Constructing the final array
    $result = array( 'product' => array_values( $result ) );
    
    
    echo "<pre>";
    print_r( $result );
    echo "</pre>";
    

    This will result to:

    Array
    (
        [product] => Array
            (
                [0] => Array
                    (
                        [product_name] => Parent Name 1
                        [product_info] => Array
                            (
                                [0] => Array
                                    (
                                        [product_name] => Product 1
                                    )
    
                                [1] => Array
                                    (
                                        [product_name] => Product 2
                                    )
    
                            )
    
                    )
    
                [1] => Array
                    (
                        [product_name] => Parent Name 2
                        [product_info] => Array
                            (
                                [0] => Array
                                    (
                                        [product_name] => Product 1
                                    )
    
                            )
    
                    )
    
                [2] => Array
                    (
                        [product_name] => Parent Name 3
                        [product_info] => Array
                            (
                                [0] => Array
                                    (
                                        [product_name] => Product 1
                                    )
    
                            )
    
                    )
    
            )
    
    )
    

    Doc: http://php.net/manual/en/function.array-reduce.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么