在数组内动态合并具有相同键的数组

时间:2016-12-28 13:24:40

标签: php arrays

我使用的是PHP 5.6。我有一个像这样的数组:

 $array = [
    0 => [
        'johndrake@gmail.com' => [
            'email' => 'johndrake@gmail.com',
            'firstname' => 'john',
            'lastname' => 'drake',
            'food' => 'burger',
        ],
    ],
    1 => [
        'johndrake@gmail.com' => [
            'email' => 'johndrake@gmail.com',
            'firstname' => 'john',
            'lastname' => 'drake',
            'drink' => 'coke',
        ],
    ],
    2 => [
        'samwin@gmail.com' => [
            'email' => 'samwin@gmail.com',
            'firstname' => 'sam',
            'lastname' => 'win',
            'food' => 'pizza',
        ],
    ],
    3 => [
        'samwin@gmail.com' => [
            'email' => 'samwin@gmail.com',
            'firstname' => 'sam',
            'lastname' => 'win',
            'drink' => 'pepsi',
        ],
    ],
];

让我们说"食物"和"饮料"是类别,它们可以根据问卷的设置方式而有所不同。事实上,可能有"食物","饮料"和"甜点"例如。如您所见,每个类别都有一个数组。我想要做的是每个人合并数组,以便它显示如下:

$array = [
    0 => [
        'johndrake@gmail.com' => [
            'email' => 'johndrake@gmail.com',
            'firstname' => 'john',
            'lastname' => 'drake',
            'food' => 'burger',
            'drink' => 'coke', // Merged
        ],
    ],
    1 => [
        'samwin@gmail.com' => [
            'email' => 'samwin@gmail.com',
            'firstname' => 'sam',
            'lastname' => 'win',
            'food' => 'pizza',
            'drink' => 'pepsi', // Merged
        ],
    ],
];

我查看了array_merge以及array_merge_recursive,但是从我读过的所有内容中,你必须指定数组才能合并它们,例如它必须是array_merge(array[0], array[1])。正如你所看到的,我无法做到这一点,因为每个人可能有2,3或甚至4个阵列必须合并在一起。

那么如何为每个人动态合并数组,以便它们一起显示所有信息呢?

2 个答案:

答案 0 :(得分:0)

这不是你要求的结果,它感觉非常混乱,但它可能是你可以进一步努力的事情:)

$result = [];

foreach ($array as $answers) {
    foreach ($answers as $email => $values) {
        if (!isset($result[$email])) {
            $result[$email] = $values;
            continue;
        }
        $result[$email] = array_merge($result[$email], $values);
    }
}

结果:

$result === [
    'johndrake@gmail.com' => [
        'email' => 'johndrake@gmail.com',
        'firstname' => 'john',
        'lastname' => 'drake',
        'food' => 'burger',
        'drink' => 'coke',
    ],
    'samwin@gmail.com' => [
        'email' => 'samwin@gmail.com',
        'firstname' => 'sam',
        'lastname' => 'win',
        'food' => 'pizza',
        'drink' => 'pepsi',
    ],
]

答案 1 :(得分:0)

<?php
$originalArray=array (
        0 => array ('johndrake@gmail.com' => array ('email' => 'johndrake@gmail.com', 'firstname' => 'john', 'lastname' => 'drake', 'food' => 'burger')),
        1 => array ('johndrake@gmail.com' => array ('email' => 'johndrake@gmail.com', 'firstname' => 'john', 'lastname' => 'drake', 'drink' => 'coke')),
        2 => array ('samwin@gmail.com' => array ('email' => 'samwin@gmail.com', 'firstname' => 'sam', 'lastname' => 'win', 'food' => 'pizza')),
        3 => array ('samwin@gmail.com' => array ('email' => 'samwin@gmail.com', 'firstname' => 'sam' ,'lastname' => 'win', 'drink' => 'pepsi'))
);
$tempArray=array();
$newArray=array();
foreach($originalArray as $key=>$val){
    $valKey=key($val);
    if(isset($tempArray[$valKey])){
        $newArray[]=array_merge($tempArray[$valKey],$val[$valKey]);
    }
    else{
        $tempArray[$valKey]=$val[$valKey];
    }
}
echo "<pre>";print_r($newArray);