【问题标题】:How to Create a Key and Bind the Array Values in Multidimensional Way PHP如何以多维方式创建键并绑定数组值 PHP
【发布时间】:2012-08-21 05:40:57
【问题描述】:

我这里有数据:

这是我的第一个函数的结果。我用于结果的变量是 $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 );

            }

但是,我需要一个更好的结果,第一个,我需要添加 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 
    )

有可能实现吗?请以正确的方式指导我,感谢您的关注,并在此先感谢您。

【问题讨论】:

  • 谁能帮帮我,我知道这对你们所有人来说都很容易。谢谢
  • 是的,有可能做到。问题已回答。请下一个。

标签: php arrays multidimensional-array array-merge


【解决方案1】:

目前,您将 ID 设置为子 ID。为了更容易识别,最好在keys中使用这些唯一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;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    相关资源
    最近更新 更多