PHP group array by 2 different values
I have an array like this:
Array
(
[0] => Array
(
[datetime] => 2010-11-06 21:25:53
[movieID] => 1197624
[personID] => 0
[more indexes]...
)
[1] => Array
(
[datetime] => 2010-11-06 21:32:56
[movieID] => 1197624
[personID] => 0
[more indexes]...
)
[2] => Array
(
[datetime] => 2010-11-06 21:38:07
[movieID] => 0
[personID] => 0987657
[more indexes]...
)
[3] => Array
(
[datetime] => 2010-11-06 21:55:09
[movieID] => 0
[personID] => 0987657
[more indexes]...
)
[4] => Array
(
[datetime] => 2010-11-06 21:59:33
[movieID] => 5467023
[personID] => 0
[more indexes]...
)
)
In each inner array, when [movieID] has a number different than 0, then [personID] will be equal to 0 and vice-versa. I want to group the array by [movieID] if its different than 0 and by [personID] if its different than 0
So the result will be something like this:
Array
(
[1197624] => Array
(
[0] => Array
(
[datetime] => 2010-11-06 21:32:56
[movieID] => 1197624
[personID] => 0
[more indexes]...
)
[1] => Array
开发者_高级运维 (
[datetime] => 2010-11-06 21:25:53
[movieID] => 1197624
[personID] => 0
[more indexes]...
)
)
[0987657] => Array
(
[0] => Array
(
[datetime] => 2010-11-06 21:38:07
[movieID] => 0
[personID] => 0987657
[more indexes]...
)
[1] => Array
(
[datetime] => 2010-11-06 21:55:09
[movieID] => 0
[personID] => 0987657
[more indexes]...
)
)
[5467023] => Array
(
[0] => Array
(
[datetime] => 2010-11-06 21:59:33
[movieID] => 5467023
[personID] => 0
[more indexes]...
)
)
)
The only problem I see by grouping like this is that there is a chance that [movieID] and [personID] are the same number (in different indexes of course) but the chance is very little.
So can someone help me grouping this array???
So essentially something like this?
$replacement = array();
foreach($original as $item) {
if ($item['movieID'] != 0)
$replacement[ $item['movieID'] ][] = $item;
else
$replacement[ $item['personID'] ][] = $item;
}
精彩评论