I have an array with common product code & product name. For each product_code can have two types of cha_sty_id i.e push or pull. This is an array structure I have.
$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"
]
];
Now I want result something like:
0 => [
'product_code' => 67021687,
'product_name' => 'Spaces.
'push => array(....ALL chs_name for push),
'pull' => array with chs_name for pull
]
I have tried some code
$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'];
}
}
But I could not solve it. Can anybody pls help me.
Thank You.