第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用多級(jí)數(shù)組重構(gòu) foreach

使用多級(jí)數(shù)組重構(gòu) foreach

PHP
紅顏莎娜 2021-11-05 13:24:16
我目前正在從一個(gè)數(shù)組中的 sql 語(yǔ)句返回結(jié)果,如下所示:$results = [];    foreach($promotionTool as $p){        $results[] = $p;    }    return $results;我的控制臺(tái)在具有以下結(jié)構(gòu)的對(duì)象中顯示:(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"       這顯示了我期望的數(shù)據(jù),但我對(duì)如何重組它有點(diǎn)迷茫,以便我可以在某些級(jí)別上進(jìn)行分組,最后只返回一個(gè)屬性數(shù)組,如下所示:    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"                   )我將如何重構(gòu)我的 foreach 以這種方式在多個(gè)級(jí)別進(jìn)行分組?
查看完整描述

1 回答

?
慕田峪4524236

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊

我想這就是你要找的:


<?php


$input = [

    [

        'codeID' => "41",

        'code' => "123ABC",

        'rule_type' => "Category",

        'attribute_type' => "identifier",

        'attribute_title' => "category number",

        'attribute_value' => "234"

    ], 

    [

        'codeID' => "41",

        'code' => "123ABC",

        'rule_type' => "Category",

        'attribute_type' => "amount",

        'attribute_title' => "percent",

        'attribute_value' => "25"

    ]

];



$output = [];

array_walk($input, function ($e) use (&$output) {

    $output[$e['codeID']][$e['code']][$e['rule_type']][] = [

        'attribute_type' => $e['attribute_type'],

        'attribute_title' => $e['attribute_title'],

        'attribute_value' => $e['attribute_value']

    ];

});


print_r($output);

這可能是一個(gè)更容易閱讀的變體:


array_walk($input, function ($e) use (&$output) {

    $codeID = &$e['codeID'];

    $code = &$e['code'];

    $rule_type = &$e['rule_type'];


    $output[$codeID][$code][$rule_type][] = [

        'attribute_type' => $e['attribute_type'],

        'attribute_title' => $e['attribute_title'],

        'attribute_value' => $e['attribute_value']

    ];

});

輸出顯然是:


Array

(

    [41] => Array

        (

            [123ABC] => Array

                (

                    [Category] => Array

                        (

                            [0] => Array

                                (

                                    [attribute_type] => identifier

                                    [attribute_title] => category number

                                    [attribute_value] => 234

                                )


                            [1] => Array

                                (

                                    [attribute_type] => amount

                                    [attribute_title] => percent

                                    [attribute_value] => 25

                                )

                        )

                )

        )

)


查看完整回答
反對(duì) 回復(fù) 2021-11-05
  • 1 回答
  • 0 關(guān)注
  • 170 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)