首页 > 解决方案 > 按键拆分和分组数组

问题描述

好吧,我已经做了一些搜索,但没有找到答案,所以我要试一试。

我有一个可以吐出以下信息的 JSON 文件:

[
    {
        "Contact name(s)": "Person 1, Person 2",
        "Contact title(s)": "Head Comacho, Other Guy",
        "Contact email(s)": "email1@email.net, email@email.com",
        "Contact phone": "123-456-7890, 789-456-1230",
    },
    {   
        "Contact name(s)": "Some Dude",
        "Contact title(s)": "Cool Title",
        "Contact email(s)": "things@email.com",
        "Contact phone": "555-555-5555",
    },
        "Contact name(s)": "",
        "Contact title(s)": "",
        "Contact email(s)": "",
        "Contact phone": "",
    }
]

不幸的是,这就是数据在出现时的命名方式。出我的手。无论如何,我必须得到它,以便拥有多个联系人的这些将分组到他们自己的数组中。这是它需要的样子:

Array
(
    [0] => Array
        (
            [contact_name] => Person 1
            [contact_title] => Head Comacho
            [contact_email] => email1@email.net
            [contact_phone] => 123-456-7890
        )

    [1] => Array
        (
            [contact_name] => Person 2
            [contact_title] => Other Title
            [contact_email] => email@email.com
            [contact_phone] => 789-456-1230
        )
)
Array
(
    [0] => Array
        (
            [contact_name] => Some Dude
            [contact_title] => Cool Title
            [contact_email] => things@email.com
            [contact_phone] => 555-555-5555
        )
)
Array
(
    [0] => Array
        (
            [contact_name] => 
            [contact_title] => 
            [contact_email] => 
            [contact_phone] => 
        )
)

我能够将数据转换为自己的数组,如下所示:

Array
(
    [contact_name] => Array
        (
            [0] => Person 1
            [1] => Person 2
        )

    [contact_title] => Array
        (
            [0] => Head Comacho
            [1] => Other Guy
        )

    [contact_email] => Array
        (
            [0] => email1@email.net
            [1] => email@email.com
        )

    [contact_phone] => Array
        (
            [0] => 123-456-7890
            [1] => 789-456-1230
        )

)

Array
(
 .....etc
)

我将如何像第一个示例一样获得它?

抱歉,如果之前已经回答过这个问题,我不确定如何表达我的问题。任何和所有的帮助表示赞赏。

标签: phparraysgrouping

解决方案


我认为这个脚本会做你想做的事。我创建了一个函数split_contacts,它获取解码数组中的每个条目,并将值(例如"Person 1, Person 2"=> [“Person 1”,“Person 2”])拆分为由对象键索引的数组,这些数组preg_replace由您想要的形式(例如"Contact name(s)" => "contact_name")。请注意,它可能可以使用str_replace而不是preg_replace,但我想保持代码在键值格式方面的灵活性。

$json = '[
    {
        "Contact name(s)": "Person 1, Person 2",
        "Contact title(s)": "Head Comacho, Other Guy",
        "Contact email(s)": "email1@email.net, email@email.com",
        "Contact phone": "123-456-7890, 789-456-1230"
    },
    {   
        "Contact name(s)": "Some Dude",
        "Contact title(s)": "Cool Title",
        "Contact email(s)": "things@email.com",
        "Contact phone": "555-555-5555"
    },
    {
        "Contact name(s)": "",
        "Contact title(s)": "",
        "Contact email(s)": "",
        "Contact phone": ""
    }
]';
$arr = json_decode($json, true);

function split_contact($contact) {
    $contacts = array();
    foreach ($contact as $key => $values) {
        $key = strtolower(preg_replace(array('/\(s\)$/', '/\s+/'), array('', '_'), $key));
        foreach (explode(',', $values) as $index => $value) {
            $contacts[$index][$key] = trim($value);
        }
    }
    return $contacts;
}

$newarr = array();
foreach ($arr as $contact) {
    $newarr[] = split_contact($contact);
}

print_r($newarr);

输出:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [contact_name] => Person 1
                    [contact_title] => Head Comacho
                    [contact_email] => email1@email.net
                    [contact_phone] => 123-456-7890
                )    
            [1] => Array
                (
                    [contact_name] =>  Person 2
                    [contact_title] =>  Other Guy
                    [contact_email] =>  email@email.com
                    [contact_phone] =>  789-456-1230
                )    
        )    
    [1] => Array
        (
            [0] => Array
                (
                    [contact_name] => Some Dude
                    [contact_title] => Cool Title
                    [contact_email] => things@email.com
                    [contact_phone] => 555-555-5555
                )    
        )    
    [2] => Array
        (
            [0] => Array
                (
                    [contact_name] => 
                    [contact_title] => 
                    [contact_email] => 
                    [contact_phone] => 
                )    
        )    
)

推荐阅读