0

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.

Mic
  • 81
  • 2
  • 12

1 Answers1

1

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;
});
DaoWen
  • 32,589
  • 6
  • 74
  • 101
  • If you're not sure what the `use` keyword does then you might want to check this post for details: [In Php 5.3.0 what is the Function "Use" Identifier?...](http://stackoverflow.com/questions/1065188/in-php-5-3-0-what-is-the-function-use-identifier-should-a-sane-programmer-us) – DaoWen Aug 17 '12 at 08:24
  • @ DaoWen, there's an error within array_walk, kindly review those brackets and parethesis. Thank you – Mic Aug 17 '12 at 08:27
  • @Mic - There is no error on my end. Are you using PHP4 or PHP5? The anonymous functions might need to be refactored out into named functions in PHP4 to get this to work. – DaoWen Aug 17 '12 at 08:29
  • @ DaoWen, Im using the netbeans, I guess it supports PHP 5, am I lost of answer? – Mic Aug 17 '12 at 08:32
  • @Mic - Try running this: `echo phpversion();`. Mine gives `5.3.8`. – DaoWen Aug 17 '12 at 08:35
  • @ DaoWen, here's the result of echo, 5.3.2-1ubuntu4.17 – Mic Aug 17 '12 at 08:38
  • @Mic - Closures have been supported since 5.3.0, so I don't see what the issue is. Try pasting my code above into its own file and running it in the terminal with the php command. It might also help if you post your full error message at the bottom of your question. – DaoWen Aug 17 '12 at 08:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15454/discussion-between-mic-and-daowen) – Mic Aug 17 '12 at 08:52
  • @ DaoWen, I move our conversation at the chat. Pls response. Thank you. – Mic Aug 17 '12 at 08:55
  • @Mic - I've posted a new version using `foreach` instead of `array_map`. This should work even in much older versions of PHP. Let me know if there are still issues and we can chat. – DaoWen Aug 17 '12 at 08:55
  • @ DaoWen, Ok I'll try it.Pls hold on. – Mic Aug 17 '12 at 08:59