How to combine the mass with the same key
-
There's a problem, getting a mass after a request for a species:
Array ( [0] => Array ( [id] => 14 [name] => 2222 [secondname] => 2222 [midname] => 2222 [username] => [email] => [gender] => М [ihr] => 222 [official_salary] => 222 [DoB] => 2021-10-14 [salary] => 222 [id_spec] => 4 [OE] => Нет [Fired] => [phone] => +7(999) 999-99-99 [isb] => Нет [id_status] => [work_shift] => 0 [id_extradition] => 186 [date_time] => 2021-11-08 00:00:00 [data_create] => 2021-11-08 [value] => 12 [value_accrued] => nal [id_komy] => 14 [name_edit] => 1 [value_accrued_nal] => 12000 [value_accrued_beznal] => )
[1] => Array ( [id] => 14 [name] => 2222 [secondname] => 2222 [midname] => 2222 [username] => [email] => [gender] => М [ihr] => 222 [official_salary] => 222 [DoB] => 2021-10-14 [salary] => 222 [id_spec] => 4 [OE] => Нет [Fired] => [phone] => +7(999) 999-99-99 [isb] => Нет [id_status] => [work_shift] => 0 [id_extradition] => 193 [date_time] => 2021-11-08 00:00:00 [data_create] => 2021-11-08 [value] => 12 [value_accrued] => beznal [id_komy] => 14 [name_edit] => 1 [value_accrued_beznal] => 40000 [value_accrued_nal] => ) [2] => Array ( [id] => 11 [name] => qwe [secondname] => qwe [midname] => qwe [username] => [email] => [gender] => М [ihr] => 231 [official_salary] => 1231 [DoB] => 2010-11-11 [salary] => 12314 [id_spec] => 4 [OE] => Нет [Fired] => [phone] => +7(999) 999-99-99 [isb] => Нет [id_status] => [work_shift] => 0 [id_extradition] => 187 [date_time] => 2021-11-08 00:00:00 [data_create] => 2021-11-08 [value] => 12 [value_accrued] => nal [id_komy] => 11 [name_edit] => 1 [value_accrued_nal] => 12000 [value_accrued_beznal] => ) [3] => Array ( [id] => 11 [name] => qwe [secondname] => qwe [midname] => qwe [username] => [email] => [gender] => М [ihr] => 231 [official_salary] => 1231 [DoB] => 2010-11-11 [salary] => 12314 [id_spec] => 4 [OE] => Нет [Fired] => [phone] => +7(999) 999-99-99 [isb] => Нет [id_status] => [work_shift] => 0 [id_extradition] => 192 [date_time] => 2021-11-08 00:00:00 [data_create] => 2021-11-08 [value] => 12 [value_accrued] => beznal [id_komy] => 11 [name_edit] => 1 [value_accrued_beznal] => 40000 [value_accrued_nal] => )
)
If the mass is carefully considered, it can be seen that some values are repeated, and they have almost all the data identical, but there is one difference, the field [value_accrued] = tie, it can only have two nal values, beznal, how I get the type of mass:
Array
(
[0] => Array
(
[id] => 14
[name] => 2222
[secondname] => 2222
[midname] => 2222
[username] =>
[email] =>
[gender] => М
[ihr] => 222
[official_salary] => 222
[DoB] => 2021-10-14
[salary] => 222
[id_spec] => 4
[OE] => Нет
[Fired] =>
[phone] => +7(999) 999-99-99
[isb] => Нет
[id_status] =>
[work_shift] => 0
[id_extradition] => 186
[date_time] => 2021-11-08 00:00:00
[data_create] => 2021-11-08
[value] => 12
[value_accrued] => nal, beznal
[id_komy] => 14
[name_edit] => 1
[value_accrued_nal] => 12000
[value_accrued_beznal] => 40000
)[1] => Array ( [id] => 11 [name] => qwe [secondname] => qwe [midname] => qwe [username] => [email] => [gender] => М [ihr] => 231 [official_salary] => 1231 [DoB] => 2010-11-11 [salary] => 12314 [id_spec] => 4 [OE] => Нет [Fired] => [phone] => +7(999) 999-99-99 [isb] => Нет [id_status] => [work_shift] => 0 [id_extradition] => 187 [date_time] => 2021-11-08 00:00:00 [data_create] => 2021-11-08 [value] => 12 [value_accrued] => nal, beznal [id_komy] => 11 [name_edit] => 1 [value_accrued_nal] => 12000 [value_accrued_beznal] => 40000 )
)
I've tried everything.
-
You have a field in your body.
id
probably acting as an identifier, and it should be grouped.
You're going down the range, rolling the elements into a new set using this id as a key. If there's a key in the body, you'll add the required field.Got something like that.
$result = []; foreach($data as $d){ $id = $d['id']; if(!isset($result[$id])){ $result[$id] = $d; $result[$id]['value_accrued'] = [ $d['value_accrued'] ]; } else{ $result[$id]['value_accrued'][] = $d['value_accrued']; } }