如何在多维PHP中创建键和绑定数组值


How to Create a Key and Bind the Array Values in Multidimensional Way PHP

我在这里有数据:

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

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->组合。

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

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

结果应该是这样的:

 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 
    )

有可能做到吗?请以适当的方式引导我,我感谢你的关心,并提前感谢你。

目前您已将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;
  }
}