2016-06-08 96 views
0

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

$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的文档,但无法找到任何东西,将做到这一点为了我。任何人都可以将我指向正确的方向吗?我甚至不知道我想在这里实现的合适搜索词,“阵列合并”并不够具体。

+0

所以首先你想知道每个子数组应该在结果中具有哪些键。为此,您可以获得每个数组的键('array_keys()')并将它们放在一起(array_merge()'+'array_unique()')。然后,您可以通过使用带有所有键的数组将每个数组放入最终数组,并填写您拥有的值('array_replace()')。 – Rizier123

回答

1

这里是我会做:

  1. 使用array_merge
  2. 使用array_keys
  3. 对于每一个单独的数组中获得这一新阵列的唯一键合并的独立数组到一个(成一个临时变量) ,循环遍历新的键数组并为每个不在数组中的键添加一个空值。然后将单独的数组推入最终数组中。
+0

谢谢,我只是这样做,它的工作,已张贴它作为一个答案,因为它帮助其他人。 – rorymorris89

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); 
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); 
0

什么贾斯汀·鲍威尔概述基础,我设法想出这个代码的另外两个代码的例子是前张贴(谢谢你mam32 & 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>'; 
+0

我觉得$ entries [$ i] [$ key] [0];是一个拼写错误,除非你想存储子数组值的第一个字母只有 – SML

+0

公平点,真正的数据实际上来自WordPress和值,即'约翰'实际上是一个单行数组,像'array( '约翰')' - 编辑,谢谢:) – rorymorris89

相关问题