2016-06-08 80 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

相關問題