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

时间:2012-05-05 02:41:32

标签: php arrays array-intersect

我有许多包含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和industries。

需要最终数组:

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(键)列表,然后遍历该数组,从我们比较的每个原始数组中提取重量和计数信息。

1 个答案:

答案 0 :(得分:0)

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

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

        )

)