2015-02-09 44 views
1

我有三個陣列(是關於數據遷移)合併3個陣列以一個基座上場

$a = Array 
    (
     [0] => Array 
     (
      [0] => province 
      [1] => 701 
      [2] => AA 
      [3] => A 
     ) 
    .. 
) 
$b = Array 
(
    [0] => Array 
     (
      [0] => district 
      [1] => 70101 
      [2] => BB 
      [3] => B 
     ) 
    [1] => Array 
     (
      [0] => district 
      [1] => 70102 
      [2] => BB1 
      [3] => B1 
     ) 
    .. 
) 


$c = Array 
(
    [0] => Array 
     (
      [0] => commune 
      [1] => 7010101 
      [2] => CC 
      [3] => C 
     ), 
    [1] => Array 
     (
      [0] => commune 
      [1] => 7010102 
      [2] => CC1 
      [3] => C1 
     ) 
.. 
) 

我想是合併所有$a$b, 7010101`

所以最後陣列可以是這個樣子:

$d = array (
       701=>array(
         70101=>array(7010101,7010102), 
         70102=>array(7010201,7010202), 
        ), 
      ) 
+0

什麼是你的問題?你試過什麼了?如果你的問題是如何做到這一點 - 我想 - 然後你只需要遍歷3個數組,並檢查(區,公社)的鍵是否包含在上面的類別例如對於一個分區,你會檢查密鑰是否以比省數字相同的3位開始 - >如果是這樣:填充你的緩衝區元素 - >與公社/區相同。 – 2015-02-09 09:12:32

+0

我想用if(strpos($ a,'701')!== false){ //做點什麼 } – kn3l 2015-02-09 09:19:48

+0

你不能在數組上應用strpos。它的字符串。 – 2015-02-09 09:24:43

回答

2

的在誘惑是這樣的:

# Your data structure here: 
$a = array(
    '701' => 'foo', 
    '702' => 'bar', 
); 

$b = array(
    '70101' => 'foo-foo', 
    '70102' => 'foo-bar', 
); 

$c = array(
    '7010101' => 'foo-foo-foo', 
    '7010102' => 'foo-foo-bar', 
    '7020101' => 'bar-foo-foo', 
    '7020201' => 'bar-bar-foo', 
); 

# The array you want 
$buffer = array(); 

# Loop through the deepest elements (here: commune) 
foreach ($c as $key => $value) { 
    # Find the keys for the parent groups 
    $province_key = substr($key, 0, 3); 
    $district_key = substr($key, 0, 5); 

    # Fill the buffer 
    $buffer[$province_key][$district_key][$key] = $value; 
} 

# Debug: The generated array 
echo '<pre>'; 
print_r($buffer); 
echo '</pre>'; 

您可以複製&粘貼here和命中運行。

0

測試數據:

$arrayA = [ 
    [ 
     0 => 'province', 
     1 => 701, 
     2 => 'AA', 
     3 => 'A' 
    ], 
    [ 
     0 => 'province', 
     1 => 702, 
     2 => 'AA1', 
     3 => 'A1' 
    ], 
]; 
$arrayB = [ 
    [ 
     0 => 'district', 
     1 => 70102, 
     2 => 'BB', 
     3 => 'B' 
    ], 
    [ 
     0 => 'district', 
     1 => 70101, 
     2 => 'BB1', 
     3 => 'B1' 
    ], 
]; 
$arrayC = [ 
    [ 
     0 => 'commune', 
     1 => 7010101, 
     2 => 'CC', 
     3 => 'C' 
    ], 
    [ 
     0 => 'commune', 
     1 => 7010102, 
     2 => 'CC1', 
     3 => 'C1' 
    ] 
]; 

解決方案:

function mergeArraysToOneOnField(array $arrayA, array $arrayB, array $arrayC, $fieldName) { 
    $result = []; 
    /* 
    checks like 
     !is_string($fieldName) && !is_integer($fieldName) 
    */ 
    $arrayARelevantFields = array_column($arrayA, $fieldName); 
    $arrayBRelevantFields = array_column($arrayB, $fieldName); 
    $arrayCRelevantFields = array_column($arrayC, $fieldName); 
    foreach ($arrayARelevantFields as $arrayARelevantField) { 
     $arrayAFilteredRelevantField = filterArrayByStrpos($arrayBRelevantFields, $arrayARelevantField); 
     foreach ($arrayAFilteredRelevantField as $arrayBRelevantField) { 
      $result[$arrayARelevantField][$arrayBRelevantField] = 
       filterArrayByStrpos($arrayCRelevantFields, $arrayBRelevantField) 
      ; 
     } 
    } 
    return $result; 
} 

試運行:

$mergedArray = mergeArraysToOneOnField($arrayA, $arrayB, $arrayC, 1); 
print_r($mergedArray); 

測試結果:

Array 
(
    [701] => Array 
     (
      [70102] => Array 
       (
       ) 

      [70101] => Array 
       (
        [0] => 7010101 
        [1] => 7010102 
       ) 

     ) 

) 

該解決方案可以用遞歸擴展,以hanlde可變數量輸入數組:

function mergeArraysToOneOnField(array &resultArray, array $inputAarray, $field) { 
    ... 
} 
-1
$array1 = array("color" => "red", 2, 4); 
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); 
$result = array_merge($array1, $array2); 

輸出

Array ([color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4) 
+0

這是'array_merge(...)'的一個很好的例子,但不是問題中描述的問題的解決方案。 -1 – automatix 2015-02-09 15:09:16