将 3 个数组合并到一个字段中

Merging 3 arrays to one base on field

我有三个数组(关于数据迁移)

$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 , $c' to become a new array in this example array that have value701is the key of sub array70101and70101is the key of sub array7010101`

因此最终数组可能如下所示:

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

尝试如下:

# 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 然后点击 运行。

测试数据:

$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
                )

        )

)

解决方案可以通过递归进行扩展,以处理可变数量的输入数组:

function mergeArraysToOneOnField(array &resultArray, array $inputAarray, $field) {
    ...
}
$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 )