使用多级数组重构 foreach

我目前正在从一个数组中的 sql 语句返回结果,如下所示:


$results = [];


    foreach($promotionTool as $p){

        $results[] = $p;

    }


    return $results;

我的控制台在具有以下结构的对象中显示:


(2) [{…}, {…}]

    0:

        codeID: "41"

        code: "123ABC"

        rule_type: "Category"

        attribute_type: "identifier"

        attribute_title: "category number"

        attribute_value: "234"

    1:

        codeID: "41"

        code: "123ABC"

        rule_type: "Category"

        attribute_type: "amount"

        attribute_title: "percent"

        attribute_value: "25"       

这显示了我期望的数据,但我对如何重组它有点迷茫,以便我可以在某些级别上进行分组,最后只返回一个属性数组,如下所示:


    codeID

        code

            rule_type

                array(

                    0:

                        attribute_type: "identifier"

                        attribute_title: "category number"

                        attribute_value: "234"

                    1:

                        attribute_type: "amount"

                        attribute_title: "percent"

                        attribute_value: "25"   

                )

我将如何重构我的 foreach 以这种方式在多个级别进行分组?


红颜莎娜
浏览 145回答 1
1回答

慕田峪4524236

我想这就是你要找的:<?php$input = [&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'codeID' => "41",&nbsp; &nbsp; &nbsp; &nbsp; 'code' => "123ABC",&nbsp; &nbsp; &nbsp; &nbsp; 'rule_type' => "Category",&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_type' => "identifier",&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_title' => "category number",&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_value' => "234"&nbsp; &nbsp; ],&nbsp;&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'codeID' => "41",&nbsp; &nbsp; &nbsp; &nbsp; 'code' => "123ABC",&nbsp; &nbsp; &nbsp; &nbsp; 'rule_type' => "Category",&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_type' => "amount",&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_title' => "percent",&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_value' => "25"&nbsp; &nbsp; ]];$output = [];array_walk($input, function ($e) use (&$output) {&nbsp; &nbsp; $output[$e['codeID']][$e['code']][$e['rule_type']][] = [&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_type' => $e['attribute_type'],&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_title' => $e['attribute_title'],&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_value' => $e['attribute_value']&nbsp; &nbsp; ];});print_r($output);这可能是一个更容易阅读的变体:array_walk($input, function ($e) use (&$output) {&nbsp; &nbsp; $codeID = &$e['codeID'];&nbsp; &nbsp; $code = &$e['code'];&nbsp; &nbsp; $rule_type = &$e['rule_type'];&nbsp; &nbsp; $output[$codeID][$code][$rule_type][] = [&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_type' => $e['attribute_type'],&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_title' => $e['attribute_title'],&nbsp; &nbsp; &nbsp; &nbsp; 'attribute_value' => $e['attribute_value']&nbsp; &nbsp; ];});输出显然是:Array(&nbsp; &nbsp; [41] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [123ABC] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [Category] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [attribute_type] => identifier&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [attribute_title] => category number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [attribute_value] => 234&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [attribute_type] => amount&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [attribute_title] => percent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [attribute_value] => 25&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))
打开App,查看更多内容
随时随地看视频慕课网APP