0

我这里有数据:

Array
(
    [3] => Array
    (
        [SiteID] => 3
        [Balance] => 94000.99
        [MinBalance] => 100000.00
        [MaxBalance] => 500000.00
        [OwnerAID] => 17
        [GroupID] => 1
        [Deposit] => 459000
        [Redemption] => 703576
        [Reload] => 169100
    )

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

    [139] => Array
    (
        [SiteID] => 139
        [Balance] => 855100.00
        [MinBalance] => 100000.00
        [MaxBalance] => 1000000.00
        [OwnerAID] => 23
        [GroupID] => 1
        [Deposit] => 0  
        [Redemption] => 0 
        [Reload] => 0 
    )
)

我需要将此阵列分为两组:第一组 - 所有 OwnerAID 拥有一个 SiteID,第二组所有 OwnerAID 拥有多个 SiteID。有可能做到吗?结果应如下所示:

这是在一个大数组中拥有一个 SiteID 的 OwnerAID 组:

Array
(
    [17] => Array
    (
        [Sites] => Array 
        (
            [0] => Array
            (
                [SiteID] => 3 
                [Balance] => 94000.99  
                [MinBalance] => 100000.00   
                [MaxBalance] => 500000.00  
                [OwnerAID] => 17  
                [GroupID] => 1 
                [Deposit] => 459000  
                [Redemption] => 703576 
                [Reload] => 169100 
            )
        )
    )    
}

在这里,OwnerAID 在另一个大数组中拥有多个 SiteID:

Array
 (
    [83] => Array
    (
        [Sites] => Array 
        (
        [0] => Array
        (
            [SiteID] => 2
            [Balance] => 19000.00
            [MinBalance] => 100000.00
            [MaxBalance] => 1000000.00
            [OwnerAID] => 83
            [GroupID] => 1
            [Deposit] => 1500
            [Redemption] => 1000
            [Reload] => 1000
        ) 
        [1] => Array
        (
            [SiteID] => 149
            [Balance] => 150000.00
            [MinBalance] => 100000.00
            [MaxBalance] => 250000.00
            [OwnerAID] => 83
            [GroupID] => 1
            [Deposit] => 0
            [Redemption] => 0
            [Reload] => 0
        )
    )
)

有人建议我这个代码,但我已经修改了它,我做了一些研究来修改这个代码。请帮助我完成这种结果。请耐心回答我的问题,我只是 PHP 编程世界的初学者。代码如下:

public function groupIndividualAndAggregateSites() 
{
    $owners = array();
    foreach($this->combined as $key => $value) {
        $owner_id = $value['OwnerAID'];
        $site_id = $value['SiteID'];
        if (array_key_exists($owner_id,$owners)) {
            // He has one or more sites already?
            if(is_array($owners[$owner_id])) {
                array_push($owners[$owner_id],$site_id);
            } else {
                // User already has one site.  
                // Make an array instead and add old and new siteID
                $old_site_id = $owners[$site_id];
                $owners[$owner_id] = array_merge((array)$value, array($old_site_id));
            }
        } else {
            $owners[$owner_id] = array($value,'Sites'=> array($site_id));
        }
    }
    print_r($owners); 
}

请帮助我,并提前谢谢你。我希望有人可以帮助我想出这种结果。请以正确的方式指导我。

4

2 回答 2

0

对不起,我发现阅读代码有点困难,所以我为你写了一个。

$new_array=array();
foreach($old_array as $site)
{
    $siteList=array();
    if(isset($new_array[$site['OwnerAID']]['Sites']))
        $siteList=$new_array[$site['OwnerAID']]['Sites'];
    $new_array=array_merge($new_array,array($site['OwnerAID']=>array_merge($siteList,$site['SiteID']=>array('Sites'=>array(
                                                 'SiteID'=>$site['SiteID'],
                                                 'Balance'=>$site['Balance'],
                                                 'MinBalance'=>$site['MinBalance'],
                                                 'MaxBalance'=>$site['MaxBalance'],
                                                 'OwnerAID'=>$site['OwnerAID'],
                                                 'GroupID'=>$site['GroupID'],
                                                 'Deposit'=>$site['Deposit'],
                                                 'Redemption'=>$site['Redemption'],
                                                 'Reload'=>$site['Reload'],
                                                 )))));
}
于 2012-08-13T07:36:21.890 回答
0

这将完全返回您想要的。

    <?php

//Array declaration
    $arr = Array
    (
        3 => Array
        (
            "SiteID" => 3,
            "Balance" => 94000,
            "MinBalance" => 100000.00,
            "MaxBalance" => 500000.00,
            "OwnerAID" => 17,
            "GroupID" => 1,
            "Deposit" => 459000,
            "Redemption" => 703576,
            "Reload" => 169100,
        ),

        2 => Array
        (
            "SiteID" => 2,
            "Balance" => 19000.00,
            "MinBalance" => 100000.00,
            "MaxBalance" => 1000000.00,
            "OwnerAID" => 83,
            "GroupID" => 1,
            "Deposit" => 1500,
            "Redemption" => 1000,
            "Reload" => 1000,
        ),

        139 => Array
        (
            "SiteID" => 139,
            "Balance" => 855100.00,
            "MinBalance" => 100000.00,
            "MaxBalance" => 1000000.00,
            "OwnerAID" => 83,
            "GroupID" => 1,
            "Deposit" => 0,
            "Redemption" => 0 ,
            "Reload" => 0 
        ),
      );


//End of declaration;
//Too lazy for other comments, sorry xD


    foreach ($arr as $key => $value) {
        $Newarr[$value["OwnerAID"]][] = $value; 
    }
    foreach ($Newarr as $key => $value) {
        foreach ($value as $key2 => $value2) {          
        if (count($value) == 1)
            $arrOneSite[$value2["OwnerAID"]]["Sites"][] = $value2;
            else    
                $arrMoreThenOneSite[$value2["OwnerAID"]]["Sites"][] = $value2;
        }
    }

    echo "<pre>";
    print_r($arrOneSite);
    print_r($arrMoreThenOneSite);
    ?>
于 2012-08-13T09:03:59.733 回答