I have data here: First, the result of my first function named getsiteaccounts()
Array
(
[0] => Array
(
[SiteID] => 2
[AID] => 5
)
[1] => Array
(
[SiteID] => 2
[AID] => 3
)
[2] => Array
(
[SiteID] => 6
[AID] => 4
)
And the result of my second function named bindGHComponentsToSites()
Array
(
[2] => Array
(
[SiteID] => 2
[Balance] => 19000.00
[MinBalance] => 100000.00
[MaxBalance] => 1000000.00
[OwnerAID] => 5
[GroupID] => 1
[Deposit] => 1500
[Reload] => 1000
[Redemption] => 1000
)
)
Then, add the key of [CorpAID] pointing to the list of OwnerAID. BY the way, OwnerAID and AID are the same. As you can see SiteID => 2 owned by two OwnerAID => 5 and 3. Here's should be the result:
Array([0])=> Array (
[SiteID] => 2
[Balance] => 19000.00
[MinBalance] => 100000.00
[MaxBalance] => 1000000.00
[OwnerAID] => 5
[GroupID] => 1
[Deposit] => 1500
[Reload] => 1000
[Redemption] => 1000
[CorpAID] => Array(
[0] => 5
[1] => 3
)
)
)
The SiteID => 6 should not be print since it only owned by one AID. Is it possible to make it? Please guide me in proper way. Thank you in advance.
You can use a couple of foreach
loops to do what you need:
<?php
$siteAccounts = array(
array('SiteID'=>2, 'AID'=>5),
array('SiteID' => 2, 'AID' => 3),
array('SiteID' => 6, 'AID' => 4)
);
$componentsToSites = array(array(
'SiteID' => 2,
'Balance' => 19000.00,
'MinBalance' => 100000.00,
'MaxBalance' => 1000000.00,
'OwnerAID' => 5,
'GroupID' => 1,
'Deposit' => 1500,
'Reload' => 1000,
'Redemption' => 1000
));
foreach ($componentsToSites as &$site) {
$aids = array();
foreach ($siteAccounts as $acct) {
if ($acct['SiteID'] === $site['SiteID'])
$aids[] = $acct['AID'];
}
$site['CorpAID'] = $aids;
}
print_r($componentsToSites);
Edit:
My original answer used array_walk
rather than foreach
, but due to compatibility issues I switched it. Here is the original code snippet for using array_walk
:
array_walk($componentsToSites, function (&$site) use ($siteAccounts) {
$aids = array();
array_walk($siteAccounts, function ($acct) use ($site, &$aids) {
if ($acct['SiteID'] === $site['SiteID'])
$aids[] = $acct['AID'];
});
$site['CorpAID'] = $aids;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.