I have data here:
Array
(
[3] => Array
(
[SiteID] => 3
[Balance] => 19000.00
[MinBalance] => 100000.00
[MaxBalance] => 1000000.00
[OwnerAID] => 17
[GroupID] => 1
[Deposit] => 1500
[Redemption] => 1000
[Reload] => 1000
)
[3] => Array
(
[SiteID] => 3
[Balance] => 19000.00
[MinBalance] => 100000.00
[MaxBalance] => 1000000.00
[OwnerAID] => 211
[GroupID] => 1
[Deposit] => 1500
[Redemption] => 1000
[Reload] => 1000
)
[4] => Array
(
[SiteID] => 4
[Balance] => 19000.00
[MinBalance] => 100000.00
[MaxBalance] => 1000000.00
[OwnerAID] => 17
[GroupID] => 1
[Deposit] => 1500
[Redemption] => 1000
[Reload] => 1000
)
[4] => Array
(
[SiteID] => 4
[Balance] => 19000.00
[MinBalance] => 100000.00
[MaxBalance] => 1000000.00
[OwnerAID] => 12
[GroupID] => 1
[Deposit] => 1500
[Redemption] => 1000
[Reload] => 1000
)
[5] => Array
(
[SiteID] => 2
[Balance] => 19000.00
[MinBalance] => 100000.00
[MaxBalance] => 1000000.00
[OwnerAID] => 11
[GroupID] => 1
[Deposit] => 1500
[Redemption] => 1000
[Reload] => 1000
)
)
)
Then I need to make a new key named "CorpAID", pointing to the list of OwnerAID owning same SiteID, It means that the SiteID can be owned by one or more OwnerAID. The result should be shown like this:
Array
(
[0]=> Array
(
[SiteID] => 3
[Balance] => 19000.00
[MinBalance] => 100000.00
[MaxBalance] => 1000000.00
[OwnerAID] => 17
[GroupID] => 1
[Deposit] => 1500
[Redemption] => 1000
[Reload] => 1000
[CorpAID] => Array
(
[0] => 17
[1] => 211
)
)
[1]=> Array
(
[SiteID] => 3
[Balance] => 19000.00
[MinBalance] => 100000.00
[MaxBalance] => 1000000.00
[OwnerAID] => 17
[GroupID] => 1
[Deposit] => 1500
[Redemption] => 1000
[Reload] => 1000
[CorpAID] => Array
(
[0] => 12
[1] => 17
)
)
Is it possible to make it? Please help me, and guide me in proper way.Thank you in advance.
This is untested code, but it should be somewhat intuitive to read what's going on. I'm assuming that the SiteID
is the field to group on.
The $tmp =&$out[$record['SiteID']];
is just a shortcut to avoid typing $out[$record['SiteID']]
over and over.
$out = array();
foreach ($records as $record) {
$tmp = &$out[$record['SiteID']];
if (isset($tmp)) {
$tmp['CorpAID'][] = $record['OwnerAID'];
} else {
$tmp = $record;
$tmp['CorpAID'] = array($record['OwnerAID']);
}
}
Btw, in MySQL you could also do this to make things easier in PHP:
SELECT GROUP_CONCAT(OwnerAID) AS CorpAID
FROM ...
GROUP BY SiteID;
i suggest you to create a little function to compare key and value trough multi-dimensional array:
function findInArray($key, $value, $array){
foreach($array as $key=>$row){
if(array_key_exists($key, $row){
if($row[$key] == $value)
return $key;
}
}
return -1;
}
and then perform an array foreach
to setup the value in the correct place, we search if in the result array already exists the SiteID
, if not we add it to the set else we just see if the OwnerAID
is already in the CorpAID
array or we add it. This way we avoid duplicate entry (seems that you don't want that in the example posted):
$result_array = array();
foreach($array as $row){
$key = findInArray('SiteID', $row['SiteID'], $result_array);
if($key == -1){
$temp = $row;
$temp['CorpAID'] = array($row['OwnerAID']);
$result_array[] = $temp;
}
else {
if(findInArray('CorpAID', $row['OwnerAID'], $result_array) == -1){
$result_array[$key]['CorpAID'][] = $row['OwnerAID'];
}
}
}
To achieve what you're looking for, you need to iterate over the values and assign a new CorpAID
field to each entry. That CorpAID
field then needs to be a reference to the value by SiteID
which will be build on iteration per each OwnerAID
.
The reference is important because when you assign it to the first elements, those are not fully filled yet because as you write, there can be multiple OwnerAID
per SiteID
.
The complicated this sound, the less large is the code. As this makes use of reference, I unset them to clean up later:
$table = array();
foreach($data as &$item)
{
$item['CorpAID'] = &$table[$item['SiteID']];
$item['CorpAID'][] = $item['OwnerAID'];
}
unset($item);
unset($table);
However, you could benefit much more by separating the site information (which is duplicated often in your data) from the owner information. It can work similarly as above.