如何识别具有相同值的数组并在PHP中放入一个大数组

时间:2012-08-14 06:52:17

标签: php arrays multidimensional-array array-merge

我这里有数据:

 Array
 (
 [3] => Array
    (
        [SiteID] => 3
        [Balance] => 19000.00
        [MinBalance] => 100000.00
        [MaxBalance] => 1000000.00
        [OwnerAID] => 17
        [GroupID] => 1
        [Deposit] => 1500
        [Redemption] => 1000
        [Reload] => 1000
    )

  [3] => Array
    (
        [SiteID] => 3
        [Balance] => 19000.00
        [MinBalance] => 100000.00
        [MaxBalance] => 1000000.00
        [OwnerAID] => 211
        [GroupID] => 1
        [Deposit] => 1500
        [Redemption] => 1000
        [Reload] => 1000
    )


   [4] => Array
    (
        [SiteID] => 4
        [Balance] => 19000.00
        [MinBalance] => 100000.00
        [MaxBalance] => 1000000.00
        [OwnerAID] => 17
        [GroupID] => 1
        [Deposit] => 1500
        [Redemption] => 1000
        [Reload] => 1000
    )

    [4] => Array
    (
        [SiteID] => 4
        [Balance] => 19000.00
        [MinBalance] => 100000.00
        [MaxBalance] => 1000000.00
        [OwnerAID] => 12
        [GroupID] => 1
        [Deposit] => 1500
        [Redemption] => 1000
        [Reload] => 1000
    )

        [5] => Array
        (
            [SiteID] => 2
            [Balance] => 19000.00
            [MinBalance] => 100000.00
            [MaxBalance] => 1000000.00
            [OwnerAID] => 11
            [GroupID] => 1
            [Deposit] => 1500
            [Redemption] => 1000
            [Reload] => 1000
        )
      )
   )

然后我需要创建一个名为“CorpAID”的新密钥,指向拥有相同SiteID的OwnerAID列表,这意味着SiteID可以由一个或多个OwnerAID拥有。结果应如下所示:

Array 
(
 [0]=> Array
 (
    [SiteID] => 3
    [Balance] => 19000.00
    [MinBalance] => 100000.00
    [MaxBalance] => 1000000.00
    [OwnerAID] => 17
    [GroupID] => 1
    [Deposit] => 1500
    [Redemption] => 1000
    [Reload] => 1000
    [CorpAID] => Array
                (
                   [0] => 17
                   [1] => 211
                )
             )
   [1]=> Array
(
    [SiteID] => 3
    [Balance] => 19000.00
    [MinBalance] => 100000.00
    [MaxBalance] => 1000000.00
    [OwnerAID] => 17
    [GroupID] => 1
    [Deposit] => 1500
    [Redemption] => 1000
    [Reload] => 1000
    [CorpAID] => Array
                (
                   [0] => 12
                   [1] => 17
                )
             )          

有可能成功吗?请帮助我,并以适当的方式指导我。提前谢谢你。

3 个答案:

答案 0 :(得分:1)

这是未经测试的代码,但它应该有点直观地阅读正在发生的事情。我假设SiteID是要分组的字段。

$tmp =&$out[$record['SiteID']];只是避免一遍又一遍地输入$out[$record['SiteID']]的快捷方式。

$out = array();

foreach ($records as $record) {
    $tmp = &$out[$record['SiteID']];
    if (isset($tmp)) {
        $tmp['CorpAID'][] = $record['OwnerAID'];
    } else {
        $tmp = $record;
        $tmp['CorpAID'] = array($record['OwnerAID']);
    }
}

顺便说一下,在MySQL中你也可以这样做,以便在PHP中更轻松:

SELECT GROUP_CONCAT(OwnerAID) AS CorpAID
FROM ...
GROUP BY SiteID;

答案 1 :(得分:0)

我建议你创建一个小函数来比较多维数组的键和值:

function findInArray($key, $value, $array){
    foreach($array as $key=>$row){
      if(array_key_exists($key, $row){
           if($row[$key] == $value)
                    return $key;
         }
   }
   return -1;
}

然后执行数组foreach以在正确的位置设置值,我们搜索结果数组中是否已存在SiteID,如果不是,我们将其添加到我们刚才看到的set中如果OwnerAID已经在CorpAID数组中,或者我们添加了它。这样我们就可以避免重复输入(似乎你不希望在发布的示例中这样做):

$result_array = array();
foreach($array as $row){
 $key = findInArray('SiteID', $row['SiteID'], $result_array);
  if($key == -1){
           $temp = $row;
           $temp['CorpAID'] = array($row['OwnerAID']);
           $result_array[] = $temp;
          }
  else {
     if(findInArray('CorpAID', $row['OwnerAID'], $result_array) == -1){
            $result_array[$key]['CorpAID'][] = $row['OwnerAID'];
        }
  }
}

答案 2 :(得分:0)

要实现您正在寻找的内容,您需要迭代这些值并为每个条目分配一个新的CorpAID字段。那么CorpAID字段需要是SiteID对值的引用,该值将基于每个OwnerAID的迭代构建。

引用非常重要,因为当您将其分配给第一个元素时,这些元素尚未完全填充,因为在您编写时,每个OwnerAID可能有多个SiteID

复杂的声音,代码越小。由于这使用了引用,我将它们取消设置以便稍后清理:

$table = array();
foreach($data as &$item)
{    
    $item['CorpAID'] = &$table[$item['SiteID']];
    $item['CorpAID'][] = $item['OwnerAID'];
}
unset($item);
unset($table);

但是,通过将站点信息(通常在数据中重复)与所有者信息分开,您可以从中受益更多。它可以像上面一样工作。