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

时间:2019-05-05 09:00:33

标签: php arrays

我有一个具有共同产品代码和产品名称的数组。 对于每个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
]

我尝试了一些代码

        $list = array();
        foreach ($records as $data) {
            $list[$data['product_code']][] = $data;
            if($data['cha_sty_id'] == 'PUSH') {
                $list[$data['product_code']]['push'] = $data['chs_name'];
            } else {
                $list[$data['product_code']]['pull'] = $data['chs_name'];
            }
        }

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

谢谢。

2 个答案:

答案 0 :(得分:1)

如何将您的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中删除代码键

答案 1 :(得分:1)

您可以使用array_walkarray_push

$res = [];
array_walk($array, function($v, $k) use (&$res){ 
if(in_array($v['product_code'], array_column($res, 'product_code'))){
    array_push($res[$v['product_code']]["push"], $v['chs_name']);
    array_push($res[$v['product_code']]["pull"], $v['chs_name']);
}else{
  $res[$v['product_code']] = [
      "product_code" => $v['product_code'],
      "product_name" => $v['product_name'],
      "push" => [$v['chs_name']],
      "pull" => [$v['chs_name']]
  ];
 }
});
echo '<pre>';
print_r(array_values($res));

DEMO HERE