1

我这里有数据:

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

有可能做到吗?请帮助我,并以正确的方式指导我。提前谢谢你。

4

3 回答 3

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;
于 2012-08-14T07:47:36.330 回答
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,如果没有,我们将其添加到集合中,否则我们只是查看 是否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'];
        }
  }
}
于 2012-08-14T07:48:04.350 回答
0

要实现您要查找的内容,您需要遍历这些值并CorpAID为每个条目分配一个新字段。然后,该CorpAID字段需要是对SiteID在每个 each 的迭代中构建的值的引用OwnerAID

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

这个声音越复杂,代码就越小。由于这利用了参考,我将它们取消设置为稍后清理:

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

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

于 2012-08-14T08:33:11.537 回答