如何创建一个键并以多维方式绑定数组值PHP

时间:2012-08-21 05:40:58

标签: php arrays multidimensional-array array-merge

我这里有数据:

这是我的第一个函数的结果。我用于结果的变量是$ this-> arrays。

Array
(
[1] => Array //Transactiondetails of SiteID 1
    (
        [SiteID] => 1
        [Balance] => 2000
        [MinBalance] => 1000
        [MaxBalance] => 500
        [OwnerAID] => 1
        [GroupID] => 1
        [Deposit] => 10000
        [Reload] => 0
        [Redemption] => 0
    )
  )

现在,这是我的第二个函数的结果。我用于结果的变量是$ this-> combined。

Array
(
[0] => Array
    (
        [AID] => 1
        [Sites] => Array
            (
                [0] => 1 //List of SiteID owned by AID                   
                [1] => 5
            )
    )

[1] => Array
    (
        [AID] => 3
        [Sites] => Array
            (
                [0] => 4 //SiteID
            )
    )

[2] => Array
    (
        [AID] => 4
        [Sites] => Array
            (
                [0] => 1 //SiteID
            )
    )

 )

我尝试使用此代码:

 public function createListOfCorpOwnedSites()
 {

 foreach ($this->combined as &$site) {

            $aids = array();

            foreach ($this->result_array as $acct) {
              if ($acct['SiteID'] === $site['SiteID'])
                $aids[] = $acct['AID'];
              }
              $site['CorpAID'] = $aids;
            }

            print_r($this->combined );

            }

但是,我需要一个更好的结果,第一个,我需要添加指向由多个AID拥有的SiteID列表的CorpAID的密钥。

这应该是结果:

 Array([0]=> Array(
        [SiteID] => 1
        [Balance] => 2000
        [MinBalance] => 1000
        [MaxBalance] => 500
        [OwnerAID] => 1
        [GroupID] => 1
        [Deposit] => 10000
        [Reload] => 0
        [Redemption] => 0
        [CorpAID] => Array(
                      [0] => 1
                      [1] => 4 
    )

有可能成功吗?请以正确的方式指导我,感谢您的关注,并提前感谢您。

1 个答案:

答案 0 :(得分:0)

目前,您将ID设置为子级。最好在中使用这些唯一ID,以便更容易识别

[1] => Array (
  [SiteID] => 1
  ...
)

[1] => Array ( // SiteID
  ...
)

我会改变第二个函数的结果($ this-> combined)。

[0] => Array (
  [AID] => 1
  ...
)

[1]=> Array ( // AID ID
  ...
)

之后需要这样做

foreach($this->combined as $aid) {
  $aidID = key($aid);
  foreach($aid as $siteID) {
    $this->arrays[$siteID]['CorpAID'][] = $aidID;
  }
}