合并具有不同键值对的数组

时间:2016-06-08 15:04:26

标签: php arrays

所以我有各种各样的数组,它们并不总是具有相同的键/值对。我想要做的是能够合并数组,但是如果它们在该数组中不存在则添加空键/值对,但在其他数组中则存在。这很难解释,但这可能更好地解释它:

$arrayOne = array('name' => 'rory', 'car' => 'opel');
$arrayTwo = array('name' => 'john', 'dog' => 'albert');

我需要以某种方式将其变为:

$finalArray = array(
    array('name' => 'rory', 'car' => 'opel', 'dog' => ''),
    array('name' => 'john', 'car' => '', 'dog' => 'albert')
);

我一直在查看PHP的文档,但找不到任何可以帮我的内容。谁能指出我正确的方向?我甚至不知道我想在这里实现的搜索术语,“数组合并”不够具体。

4 个答案:

答案 0 :(得分:2)

<?php
$arrayOne = array('name' => 'rory', 'car' => 'opel');
$arrayTwo = array('name' => 'john', 'dog' => 'albert');

$diff1=array_diff(array_flip($arrayOne), array_flip($arrayTwo));
$diff2=array_diff(array_flip($arrayTwo), array_flip($arrayOne));
//array_flip flips the key of array with value
//array_diff would return the values in the first array that are not present in any of the other arrays inside

foreach ($diff2 as $s) {
    $arrayOne[$s]="";
}
foreach ($diff1 as $s) {
    $arrayTwo[$s]="";
};
//set key that didn't exist in that array as ""

$finalArray[]=$arrayOne;
$finalArray[]=$arrayTwo;
//add the arrays to the final array

print_r($finalArray);

答案 1 :(得分:1)

这就是我要做的事情:

  1. 使用array_merge
  2. 将单独的数组合并为一个(转换为临时变量)
  3. 使用array_keys
  4. 获取此新数组的唯一键
  5. 对于每个单独的数组,循环遍历新的键数组,并为每个不在数组中的键添加空值。然后将单独的数组推入最终数组。

答案 2 :(得分:1)

<?php
$arrayOne = array('name' => 'rory', 'car' => 'opel');
$arrayTwo = array('name' => 'john', 'dog' => 'albert');
$new = array_merge($arrayOne,$arrayTwo);
$new = array_keys($new);
$newarray = array();        
foreach($new as $value){            
$newarray[0][$value] = isset($arrayOne[$value]) ? $arrayOne[$value] :  '' ;
$newarray[1][$value] = isset($arrayTwo[$value]) ? $arrayTwo[$value] :  '' ;         
        }
        echo "<pre>";print_r($newarray);

您也可以使用这个简短的回答

$arrayOne = array('name' => 'rory', 'car' => 'opel');
$arrayTwo = array('name' => 'john', 'dog' => 'albert');
$defaults = array('name' => '','car'  => '','dog' => '');
$arrayOne += $defaults;
$arrayTwo += $defaults;
$newarray = array($arrayOne,$arrayTwo);
echo "<pre>";print_r($newarray);

答案 3 :(得分:0)

基于Justin Powell概述的内容,我在其他两个代码示例发布之前设法提出了这个代码(谢谢mamta&amp; user6439245)。

我还需要使用包含数字的密钥并对其进行适当排序,否则我的密钥将被编入索引,如employer_1, education_1, employer_2, education_2

// get the initial form entries data
$entries = array(
    array('name' => 'john', 'car' => 'fiat', 'employer_1' => 'tangerine', 'education_1' => 'hideaways', 'education_2' => 'extras'),
    array('name' => 'rory', 'car' => 'opel', 'employer_1' => 'sagittarius', 'employer_2' => 'tangerine', 'employer_3' => 'thehideout', 'education_1' => 'knatchbull')
);


// create an empty array to populate with all field keys
$mergedKeys = array();

// push all field keys into the array
foreach($entries as $entry){
    foreach($entry as $key => $value){
        array_push($mergedKeys, $key);
    }
}

// remove duplicate keys from the array
$uniqueMergedKeys = array_unique($mergedKeys);

// create a new array to populate with the field keys we need to sort - the ones with numbers in
$keysToSort = array();

// push the number-containing keys into the array
$i=0;
foreach($uniqueMergedKeys as $uniqueKey){
    if(1 === preg_match('~[0-9]~', $uniqueKey)){
        array_push($keysToSort, $uniqueKey);
    }
    $i++;
}

// remove the number containing keys from the unique keys array
$uniqueMergedKeys = array_diff($uniqueMergedKeys, $keysToSort);

// sort the keys that need sorting
sort($keysToSort);

// put the newly sorted keys back onto the original keys array
foreach($keysToSort as $key){
    array_push($uniqueMergedKeys, $key);
}

$final = array();

$i = 0;
foreach($entries as $entry){

    foreach($uniqueMergedKeys as $key){
        //if($entries[$i][$key]){
        if (array_key_exists($key, $entries[$i])) {
            $final[$i][$key] = $entries[$i][$key];
        } else {
            $final[$i][$key] = '';
        }
    }

    $i++;
}

echo '<pre>'; print_r($final); echo '</pre>';