Начальный массив, с которого я начал:
{
"0":{
"id":"1",
"country_id":"Austria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"herbert.mayrhofer@mazars.at",
"phone":"0"},
"4":{
"id":"11",
"country_id":"Bulgaria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"herbert.mayrhofer@mazars.at BG",
"phone":"0"},
"1":{
"id":"2",
"country_id":"Austria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"herbert.mayrhofer@mazars.at",
"phone":"0"},
"5":{
"id":"12",
"country_id":"Bulgaria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"herbert.mayrhofer@mazars.at BG",
"phone":"0"},
"2":{
"id":"3",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner",
"title":"Mag",
"email":"guenther.mayrleitner@mazars.at",
"phone":"0"},
"3":{
"id":"4",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"verena.ziegler@mazars.at",
"phone":"0"},
"6":{
"id":"13",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner BG",
"title":"Mag BG",
"email":"guenther.mayrleitner@mazars.at BG",
"phone":"0"},
"7":{
"id":"14",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena BG",
"title":"MA BG",
"email":"verena.ziegler@mazars.at BG",
"phone":"0"
}
}
У меня есть массив, который выглядит следующим образом:
{
"Business development":[
{
"id":"1",
"country_id":"Austria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"herbert.mayrhofer@mazars.at",
"phone":"0"
},{
"id":"11",
"country_id":"Bulgaria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"herbert.mayrhofer@mazars.at BG",
"phone":"0"
}
],
"Technical contact":[
{
"id":"2",
"country_id":"Austria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"herbert.mayrhofer@mazars.at",
"phone":"0"
},{
"id":"12",
"country_id":"Bulgaria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"herbert.mayrhofer@mazars.at BG",
"phone":"0"
}
],
"VAT leader":[
{
"id":"3",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner",
"title":"Mag",
"email":"guenther.mayrleitner@mazars.at",
"phone":"0"
},{
"id":"4",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"verena.ziegler@mazars.at",
"phone":"0"
},{
"id":"13",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner BG",
"title":"Mag BG",
"email":"guenther.mayrleitner@mazars.at BG",
"phone":"0"
},{
"id":"14",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena BG",
"title":"MA BG",
"email":"verena.ziegler@mazars.at BG",
"phone":"0"
}
]
}
И я хочу, чтобы это выглядело так:
{
"Business development":[
"Austria": [
"id":"1",
"country_id":"Austria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"herbert.mayrhofer@mazars.at",
"phone":"0"
],
"Bulgaria": [
"id":"11",
"country_id":"Bulgaria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"herbert.mayrhofer@mazars.at BG",
"phone":"0"
]
],
"Technical contact":[
"Austria": [
"id":"2",
"country_id":"Austria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"herbert.mayrhofer@mazars.at",
"phone":"0"
],
"Bulgaria": [
"id":"12",
"country_id":"Bulgaria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"herbert.mayrhofer@mazars.at BG",
"phone":"0"
]
],
"VAT leader":[
"Austria": [
{
"id":"3",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner",
"title":"Mag",
"email":"guenther.mayrleitner@mazars.at",
"phone":"0"
},{
"id":"4",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"verena.ziegler@mazars.at",
"phone":"0"
}
],
"Bulgaria": [
{
"id":"13",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner BG",
"title":"Mag BG",
"email":"guenther.mayrleitner@mazars.at BG",
"phone":"0"
},{
"id":"14",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena BG",
"title":"MA BG",
"email":"verena.ziegler@mazars.at BG",
"phone":"0"
}
]
]
}
Что я сделал до сих пор:
function prepare_data($array, $primary_instance, $dependency=NULL){
$handled_data = [];
$cloned = $array;
foreach ($array as $k => $v) {
foreach ($cloned as $key => $value) {
if ($k != $key && $v[$primary_instance] == $value[$primary_instance]) {
if (!in_array($value[$primary_instance], $handled_data) ) {
$handled_data[$value[$primary_instance]][] = $v;
break;
}
}
}
}
// if has dependencies, handle it
if(!is_null($dependency)){
$cloned = $handled_data;
foreach ($handled_data as $k => $v) {
foreach ($cloned as $key => $value) {
if ($v != $value && $k[$dependency] == $key[$dependency]) {
if (!in_array($key[$dependency], $handled_data) ) {
$handled_data[$dependency][][$key[$dependency]][] = $v;
break;
}
}
}
}
}
return $handled_data;
}
Как я Вообразил, что это сработает: Вызовите функцию с начальным массивом и добавьте ключи prepare_data($sorted_contacts, 'VAT_contact_person', 'country_id')
Мне удалось сделать это от наихудшего к первому примеру, используя первую часть функции без if(!is_null($dependency))
условие, но теперь я хочу использовать $ зависимость, которую я передаю в функцию для структурирования от первого примера ко второму.
РЕДАКТИРОВАТЬ: Окончательная форма возвращаемого массива должна содержать только массив
РЕДАКТИРОВАТЬ 2:
"VAT leader":{
"Austria":[
{
"id":"3",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"verena.ziegler@mazars.at",
"phone":"0"
},
{
"id":"4",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"verena.ziegler@mazars.at",
"phone":"0"
}
],
"Bulgaria":[
{
"id":"13",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"verena.ziegler@mazars.at",
"phone":"0"
},
{
"id":"14",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"verena.ziegler@mazars.at",
"phone":"0"
}
]
}