在 PHP 中执行数组交集时如何保留多维数组信息


How can I preserve multidimensional array information when performing array intersections in PHP?

我有许多数组包含 ID 作为主键,每个 id 下都有多维信息。 下面是两个示例:

第一个示例数组:

array
  14181 => 
    array
      'industries' => 
        array
          'weight' => string '105652' (length=6)
          'count' => string '11' (length=2)
  48354 => 
    array
      'industries' => 
        array
          'weight' => string '508866' (length=6)
          'count' => string '10' (length=2)

第二个示例数组:

array
  16434 => 
    array
      'business_types' => 
        array
          'weight' => string '104614' (length=6)
          'count' => string '1' (length=1)
  48354 => 
    array
      'business_types' => 
        array
          'weight' => string '103610' (length=6)
          'count' => string '10' (length=2)

我想获得许多这样的数组的交集(基于键(,但我需要为每个键保留每个数组的权重和计数数据。 请注意,每个数组的权重和计数数据不同。 在这种情况下,business_type和行业。

需要最终阵列:

array
  48354 => 
    array
      'business_types' => 
        array
          'weight' => string '103610' (length=6)
          'count' => string '10' (length=2)
      'industries' => 
        array
          'weight' => string '508866' (length=6)
          'count' => string '10' (length=2)

最初我并没有试图保留重量和计数,所以我只是在执行一个array_intersect_keys(),工作就完成了。 现在我需要保留这些数据。 我将子数组命名为不同的东西,希望array_intersect_keys()保留它,但是,它只为函数中的第一个数组保留它。

有没有首选的方法可以做这样的事情?

我能想到的唯一解决方案是将所有数组减少到最终 ID(键(的列表,然后遍历该数组,从我们比较的每个原始数组中提取权重和计数信息。

您提出的解决方案似乎很好,但您可以考虑将一个列表合并到另一个列表中,例如

<?php
$a1 = array(14181 => array('industries'     => array('weight' => "105652", 'count' => "11")),
            48354 => array('industries'     => array('weight' => "508866", 'count' => "10")));
$a2 = array(16434 => array('business_types' => array('weight' => "104614", 'count' => "1")),
            48354 => array('business_types' => array('weight' => "103610", 'count' => "10")));
//print_r($a1);
//print_r($a2);
foreach($a2 as $a2k => $a2v)
{
    // The loop below should only go through once, but if there are multiple types it will be ok
    foreach($a2v as $a2vk => $a2vv)
    {
        $a1[$a2k][$a2vk] = $a2vv;
    }
}
printf("Output:'n");
print_r($a1);
printf("Output:'n");
print_r($a1);

输出:

Output:
Array
(
    [14181] => Array
        (
            [industries] => Array
                (
                    [weight] => 105652
                    [count] => 11
                )
        )
    [48354] => Array
        (
            [industries] => Array
                (
                    [weight] => 508866
                    [count] => 10
                )
            [business_types] => Array
                (
                    [weight] => 103610
                    [count] => 10
                )
        )
    [16434] => Array
        (
            [business_types] => Array
                (
                    [weight] => 104614
                    [count] => 1
                )
        )
)

我相信这将比您提出的解决方案略快。


编辑:以上是一个数组合并类型算法,即使键在数组中不通用,也会合并数组。 下面的算法只会产生两个数组的交集。 我最初得到这个问题的错:

<?php
$a1 = array(14181 => array('industries'     => array('weight' => "105652", 'count' => "11")),
            48354 => array('industries'     => array('weight' => "508866", 'count' => "10")));
$a2 = array(16434 => array('business_types' => array('weight' => "104614", 'count' => "1")),
            48354 => array('business_types' => array('weight' => "103610", 'count' => "10")));
//print_r($a1);
//print_r($a2);
// Pass the smaller array as first argument
if(count($a1) <= count($a2))
{
    $res = my_merge($a1, $a2);
}
else
{
    $res = my_merge($a2, $a1);
}
function my_merge($a1, $a2)
{
    $res = array();
    foreach($a1 as $a1k => $a1v)
    {
        if(array_key_exists($a1k, $a2))
        {
            $res[$a1k] = array_merge($a1[$a1k], $a2[$a1k]);
        }
    }
    return $res;
}
printf("Output:'n");
print_r($res);

输出:

Array
(
    [48354] => Array
        (
            [industries] => Array
                (
                    [weight] => 508866
                    [count] => 10
                )
            [business_types] => Array
                (
                    [weight] => 103610
                    [count] => 10
                )
        )
)