3

我有许多包含一个 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 (keys) 的列表,然后循环遍历该数组,从我们比较的每个原始数组中提取权重和计数信息。

4

1 回答 1

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
                )

        )

)
于 2012-05-05T03:00:48.877 回答