根据通用值对数组进行分组

我有一个具有通用产品代码和产品名称的数组。对于每个product_code,可以具有两种cha_sty_id类型,即push或pull。这是我拥有的数组结构。


$array = [

            0 => [

                "product_code" => "67021687",

                "product_name" => "Spaces",

                "cha_sty_id" => "PUSH",

                "chs_name" => "WF"

            ],

            1 => [

                "product_code" => "67021687",

                "product_name" => "Spaces",

                "cha_sty_id" => "PUSH",

                "chs_name" => "WFR"

            ],

            2 => [

                "product_code" => "67021687",

                "product_name" => "Spaces",

                "cha_sty_id" => "PUSH",

                "chs_name" => "STK Food"

            ],

            3 => [

                "product_code" => "67021687",

                "product_name" => "Spaces",

                "cha_sty_id" => "PULL",

                "chs_name" => "4 Stars"

            ],

            4 => [

                "product_code" => "67021687",

                "product_name" => "Spaces",

                "cha_sty_id" => "PULL",

                "chs_name" => "5 Stars"

            ],

            5 => [

                "product_code" => "67021687",

                "product_name" => "Spaces",

                "cha_sty_id" => "PULL",

                "chs_name" => "Modern Thai"

            ],

            6 => [

                "product_code" => "67021687",

                "product_name" => "Spaces",

                "cha_sty_id" => "PULL",

                "chs_name" => "BBQ Buffet"

            ],

            7 => [

                "product_code" => "67021687",

                "product_name" => "Spaces",

                "cha_sty_id" => "PULL",

                "chs_name" => "Chinese"

            ]

        ];

现在我想要的结果是这样的:


0 => [

    'product_code' => 67021687,

    'product_name' => 'Spaces.

    'push => array(....ALL chs_name for push),

    'pull' => array with chs_name for pull

]


但是我无法解决。有人可以帮助我吗?


蝴蝶刀刀
浏览 142回答 2
2回答

有只小跳蛙

如何将您的foreach循环修改为此:$list = array();foreach ($records as $data) {    $code = $data['product_code']; // as key    if (!isset($list[$code])) { // set new array if not exist        $list[$code] = array("product_code" => $code, "product_name" => $data['product_name'], "push" => [], "pull" => []);    }    $subKey = strtolower($data['cha_sty_id']); // get push / pull as new subkey    $list[$code][$subKey][] = $data['chs_name']; // append to the array}如果不需要,您可以用来array_values从$list循环后删除代码键

红颜莎娜

您可以使用array_walk,array_push$res = [];array_walk($array, function($v, $k) use (&$res){&nbsp;if(in_array($v['product_code'], array_column($res, 'product_code'))){&nbsp; &nbsp; array_push($res[$v['product_code']]["push"], $v['chs_name']);&nbsp; &nbsp; array_push($res[$v['product_code']]["pull"], $v['chs_name']);}else{&nbsp; $res[$v['product_code']] = [&nbsp; &nbsp; &nbsp; "product_code" => $v['product_code'],&nbsp; &nbsp; &nbsp; "product_name" => $v['product_name'],&nbsp; &nbsp; &nbsp; "push" => [$v['chs_name']],&nbsp; &nbsp; &nbsp; "pull" => [$v['chs_name']]&nbsp; ];&nbsp;}});echo '<pre>';print_r(array_values($res));此处演示
打开App,查看更多内容
随时随地看视频慕课网APP