处理PHP多维数组以将键分配为值数组的父级

问题描述 投票:1回答:1

我开始使用的初始数组:

{
    "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))条件,从最坏的情况到第一个示例,但是现在,我想使用$ 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"
            }
        ]
    }
php arrays multidimensional-array
1个回答
1
投票

为了做到这一点,您需要按照以下步骤重建数组(从原始数组开始...

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

var_dump($finalData);

从原始平面数组开始,您正在创建两个分类法:VAT_contact_personcountry_id。只需将列表遍历一次,然后以这种幌子将它们插入到新数组中,便可以使平面列表中的每个元素都完全适合这些分类法。

所以结果:

数组(3){[“业务发展”] =>数组(2){[“奥地利”] =>数组(1){[0] =>数组(7){[“ id”] =>字符串(1)“ 1”[“ country_id”] =>string(7)“奥地利”[“ VAT_contact_person”] =>string(20)“业务发展”[“名称”] =>字符串(17)“ Mayrhofer Herbert”[“标题”] =>string(4)“魔术”。[“电子邮件”] =>字符串(27)“ herbert.mayrhofer@mazars.at”[“电话”] =>字符串(1)“ 0”}}[“保加利亚”] =>数组(1){[0] =>数组(7){[“ id”] =>字符串(2)“ 11”[“ country_id”] =>string(8)“保加利亚”[“ VAT_contact_person”] =>string(20)“业务发展”[“名称”] =>字符串(20)“ Mayrhofer Herbert BG”[“标题”] =>字符串(7)“ Mag。BG”[“电子邮件”] =>字符串(30)“ herbert.mayrhofer@mazars.at BG”[“电话”] =>字符串(1)“ 0”}}}[“技术联系人”] =>数组(2){[“奥地利”] =>数组(1){[0] =>数组(7){[“ id”] =>字符串(1)“ 2”[“ country_id”] =>string(7)“奥地利”[“ VAT_contact_person”] =>字符串(17)“技术联系”[“名称”] =>字符串(17)“ Mayrhofer Herbert”[“标题”] =>string(4)“魔术”。[“电子邮件”] =>字符串(27)“ herbert.mayrhofer@mazars.at”[“电话”] =>字符串(1)“ 0”}}[“保加利亚”] =>数组(1){[0] =>数组(7){[“ id”] =>字符串(2)“ 12”[“ country_id”] =>string(8)“保加利亚”[“ VAT_contact_person”] =>字符串(17)“技术联系”[“名称”] =>字符串(20)“ Mayrhofer Herbert BG”[“标题”] =>字符串(7)“ Mag。BG”[“电子邮件”] =>字符串(30)“ herbert.mayrhofer@mazars.at BG”[“电话”] =>字符串(1)“ 0”}}}[“增值税负责人”] =>数组(2){[“奥地利”] =>数组(2){[0] =>数组(7){[“ id”] =>字符串(1)“ 3”[“ country_id”] =>string(7)“奥地利”[“ VAT_contact_person”] =>字符串(10)“增值税负责人”[“名称”] =>字符串(20)“GüntherMayrleitner”[“标题”] =>string(3)“ Mag”[“电子邮件”] =>字符串(30)“ guenther.mayrleitner@mazars.at”[“电话”] =>字符串(1)“ 0”}[1] =>数组(7){[“ id”] =>字符串(1)“ 4”[“ country_id”] =>string(7)“奥地利”[“ VAT_contact_person”] =>字符串(10)“增值税负责人”[“名称”] =>字符串(14)“ Ziegler Verena”[“标题”] =>字符串(2)“ MA”[“电子邮件”] =>字符串(24)“ verena.ziegler@mazars.at”[“电话”] =>字符串(1)“ 0”}}[“保加利亚”] =>数组(2){[0] =>数组(7){[“ id”] =>字符串(2)“ 13”[“ country_id”] =>string(8)“保加利亚”[“ VAT_contact_person”] =>字符串(10)“增值税负责人”[“名称”] =>字符串(23)“GüntherMayrleitner BG”[“标题”] =>字符串(6)“ Mag BG”[“电子邮件”] =>字符串(33)“ guenther.mayrleitner@mazars.at BG”[“电话”] =>字符串(1)“ 0”}[1] =>数组(7){[“ id”] =>字符串(2)“ 14”[“ country_id”] =>string(8)“保加利亚”[“ VAT_contact_person”] =>字符串(10)“增值税负责人”[“名称”] =>字符串(17)“ Ziegler Verena BG”[“标题”] =>字符串(5)“ MA BG”[“电子邮件”] =>字符串(27)“ verena.ziegler@mazars.at BG”[“电话”] =>字符串(1)“ 0”}}}}
热门问题
推荐问题
最新问题