Initial array i've started with:
{
"0":{
"id":"1",
"country_id":"Austria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"[email protected]",
"phone":"0"},
"4":{
"id":"11",
"country_id":"Bulgaria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"[email protected] BG",
"phone":"0"},
"1":{
"id":"2",
"country_id":"Austria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"[email protected]",
"phone":"0"},
"5":{
"id":"12",
"country_id":"Bulgaria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"[email protected] BG",
"phone":"0"},
"2":{
"id":"3",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner",
"title":"Mag",
"email":"[email protected]",
"phone":"0"},
"3":{
"id":"4",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"[email protected]",
"phone":"0"},
"6":{
"id":"13",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner BG",
"title":"Mag BG",
"email":"[email protected] BG",
"phone":"0"},
"7":{
"id":"14",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena BG",
"title":"MA BG",
"email":"[email protected] BG",
"phone":"0"
}
}
I've got an array that looks like:
{
"Business development":[
{
"id":"1",
"country_id":"Austria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"[email protected]",
"phone":"0"
},{
"id":"11",
"country_id":"Bulgaria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"[email protected] BG",
"phone":"0"
}
],
"Technical contact":[
{
"id":"2",
"country_id":"Austria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"[email protected]",
"phone":"0"
},{
"id":"12",
"country_id":"Bulgaria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"[email protected] BG",
"phone":"0"
}
],
"VAT leader":[
{
"id":"3",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner",
"title":"Mag",
"email":"[email protected]",
"phone":"0"
},{
"id":"4",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"[email protected]",
"phone":"0"
},{
"id":"13",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner BG",
"title":"Mag BG",
"email":"[email protected] BG",
"phone":"0"
},{
"id":"14",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena BG",
"title":"MA BG",
"email":"[email protected] BG",
"phone":"0"
}
]
}
And i want it to look like:
{
"Business development":[
"Austria": [
"id":"1",
"country_id":"Austria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"[email protected]",
"phone":"0"
],
"Bulgaria": [
"id":"11",
"country_id":"Bulgaria",
"VAT_contact_person":"Business development",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"[email protected] BG",
"phone":"0"
]
],
"Technical contact":[
"Austria": [
"id":"2",
"country_id":"Austria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert",
"title":"Mag.",
"email":"[email protected]",
"phone":"0"
],
"Bulgaria": [
"id":"12",
"country_id":"Bulgaria",
"VAT_contact_person":"Technical contact",
"name":"Mayrhofer Herbert BG",
"title":"Mag. BG",
"email":"[email protected] BG",
"phone":"0"
]
],
"VAT leader":[
"Austria": [
{
"id":"3",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner",
"title":"Mag",
"email":"[email protected]",
"phone":"0"
},{
"id":"4",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"[email protected]",
"phone":"0"
}
],
"Bulgaria": [
{
"id":"13",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"G\u00fcnther Mayrleitner BG",
"title":"Mag BG",
"email":"[email protected] BG",
"phone":"0"
},{
"id":"14",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena BG",
"title":"MA BG",
"email":"[email protected] BG",
"phone":"0"
}
]
]
}
What i did so far:
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;
}
How i imagined it would work: Call the function with initial array and add the keys prepare_data($sorted_contacts, 'VAT_contact_person', 'country_id')
I've managed to make it from worst to the first example using the first part of the function without if(!is_null($dependency)) condition but now, i want to use the $dependency i'm passing in the function to structure from first example to the second example.
EDIT: The final form of returned array should contain only array
EDIT 2:
"VAT leader":{
"Austria":[
{
"id":"3",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"[email protected]",
"phone":"0"
},
{
"id":"4",
"country_id":"Austria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"[email protected]",
"phone":"0"
}
],
"Bulgaria":[
{
"id":"13",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"[email protected]",
"phone":"0"
},
{
"id":"14",
"country_id":"Bulgaria",
"VAT_contact_person":"VAT leader",
"name":"Ziegler Verena",
"title":"MA",
"email":"[email protected]",
"phone":"0"
}
]
}