Обрабатывать PHP многомерный массив для назначения ключа в качестве родителя массива значений - PullRequest
2 голосов
/ 24 января 2020

Начальный массив, с которого я начал:

{
    "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"
            }
        ]
    }

1 Ответ

2 голосов
/ 24 января 2020

Итак, если мы начнем с этого массива ...

$data = json_decode('
{
    "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"
    }
}
', true);

Затем мы сможем построить новый многомерный массив с двумя таксономиями: а именно: VAT_contact_person и country_id просто вставив каждый элемент в список с этими значениями в качестве ключей в новом массиве.

Примерно так ...

foreach ($data as $v) {
    $finalData[$v["VAT_contact_person"]][$v["country_id"]][] = $v;
}

var_dump($finalData);

Итак, результат:

array(3) {
  ["Business development"]=>
  array(2) {
    ["Austria"]=>
    array(1) {
      [0]=>
      array(7) {
        ["id"]=>
        string(1) "1"
        ["country_id"]=>
        string(7) "Austria"
        ["VAT_contact_person"]=>
        string(20) "Business development"
        ["name"]=>
        string(17) "Mayrhofer Herbert"
        ["title"]=>
        string(4) "Mag."
        ["email"]=>
        string(27) "herbert.mayrhofer@mazars.at"
        ["phone"]=>
        string(1) "0"
      }
    }
    ["Bulgaria"]=>
    array(1) {
      [0]=>
      array(7) {
        ["id"]=>
        string(2) "11"
        ["country_id"]=>
        string(8) "Bulgaria"
        ["VAT_contact_person"]=>
        string(20) "Business development"
        ["name"]=>
        string(20) "Mayrhofer Herbert BG"
        ["title"]=>
        string(7) "Mag. BG"
        ["email"]=>
        string(30) "herbert.mayrhofer@mazars.at BG"
        ["phone"]=>
        string(1) "0"
      }
    }
  }
  ["Technical contact"]=>
  array(2) {
    ["Austria"]=>
    array(1) {
      [0]=>
      array(7) {
        ["id"]=>
        string(1) "2"
        ["country_id"]=>
        string(7) "Austria"
        ["VAT_contact_person"]=>
        string(17) "Technical contact"
        ["name"]=>
        string(17) "Mayrhofer Herbert"
        ["title"]=>
        string(4) "Mag."
        ["email"]=>
        string(27) "herbert.mayrhofer@mazars.at"
        ["phone"]=>
        string(1) "0"
      }
    }
    ["Bulgaria"]=>
    array(1) {
      [0]=>
      array(7) {
        ["id"]=>
        string(2) "12"
        ["country_id"]=>
        string(8) "Bulgaria"
        ["VAT_contact_person"]=>
        string(17) "Technical contact"
        ["name"]=>
        string(20) "Mayrhofer Herbert BG"
        ["title"]=>
        string(7) "Mag. BG"
        ["email"]=>
        string(30) "herbert.mayrhofer@mazars.at BG"
        ["phone"]=>
        string(1) "0"
      }
    }
  }
  ["VAT leader"]=>
  array(2) {
    ["Austria"]=>
    array(2) {
      [0]=>
      array(7) {
        ["id"]=>
        string(1) "3"
        ["country_id"]=>
        string(7) "Austria"
        ["VAT_contact_person"]=>
        string(10) "VAT leader"
        ["name"]=>
        string(20) "Günther Mayrleitner"
        ["title"]=>
        string(3) "Mag"
        ["email"]=>
        string(30) "guenther.mayrleitner@mazars.at"
        ["phone"]=>
        string(1) "0"
      }
      [1]=>
      array(7) {
        ["id"]=>
        string(1) "4"
        ["country_id"]=>
        string(7) "Austria"
        ["VAT_contact_person"]=>
        string(10) "VAT leader"
        ["name"]=>
        string(14) "Ziegler Verena"
        ["title"]=>
        string(2) "MA"
        ["email"]=>
        string(24) "verena.ziegler@mazars.at"
        ["phone"]=>
        string(1) "0"
      }
    }
    ["Bulgaria"]=>
    array(2) {
      [0]=>
      array(7) {
        ["id"]=>
        string(2) "13"
        ["country_id"]=>
        string(8) "Bulgaria"
        ["VAT_contact_person"]=>
        string(10) "VAT leader"
        ["name"]=>
        string(23) "Günther Mayrleitner BG"
        ["title"]=>
        string(6) "Mag BG"
        ["email"]=>
        string(33) "guenther.mayrleitner@mazars.at BG"
        ["phone"]=>
        string(1) "0"
      }
      [1]=>
      array(7) {
        ["id"]=>
        string(2) "14"
        ["country_id"]=>
        string(8) "Bulgaria"
        ["VAT_contact_person"]=>
        string(10) "VAT leader"
        ["name"]=>
        string(17) "Ziegler Verena BG"
        ["title"]=>
        string(5) "MA BG"
        ["email"]=>
        string(27) "verena.ziegler@mazars.at BG"
        ["phone"]=>
        string(1) "0"
      }
    }
  }
}
...