I have this type array
Array
(
[0] => Array
(
[id] => 90
[name] => Paul
[company] => Google
[date] => 2018-01-06
[total] => 100.00
)
[1] => Array
(
[id] => 90
[name] => Paul
[company] => Google
[date] => 2018-07-06
[total] => 100.00
)
[2] => Array
(
[id] => 89
[name] => Ethan
[company] => Yahoo
[date] => 2018-07-10
[total] => 1140.00
)
)
I need to create a new array from the previous one by merging id
and name
if they are the same.
The desired output should be:
[[
[id] => 90
[name] => Paul
[company] => Google,
[data] => [
[date] => 2018-01-06,
[total] => 100.00
],
[
[date] => 2018-07-06,
[total] => 100.00
],
], [
[id] => 89
[name] => Ethan
[company] => Yahoo
[data] => [
[date] => 2018-07-10,
[total] => 1140.00
]
]]
What I have tried before:
$output = array();
foreach($input as $data){
$output[$data['id']][] = $data;
$output[$data['name']][] = $data;
$output[$data['company']][] = $data;
}
What I'm missing here please ?
Thanks.
This should be something like what you're looking for:
foreach ($arr as $a) {
if (empty($resp[$a['id']])) {
$resp[$a['id']] = [
'id' => $a['id'],
'name' => $a['name'],
'company' => $a['company'],
'data' => []
];
}
$resp[$a['id']]['data'][] = [
'date' => $a['date'],
'total' => $a['total']
];
}
This is assuming that you only care about grouping the date and total into a repeated company and name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.