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???

link|flag

2 Answers

up vote 2 down vote accepted

So essentially something like this?

$replacement = array();
foreach($original as $item) {
    if ($item['movieID'] != 0)
        $replacement[ $item['movieID'] ][] = $item;
    else
        $replacement[ $item['personID'] ][] = $item;
}
link|flag
Nice, I expected a much bigger function. This is working great! I will give you the Answer. What you recommend to avoid a problem in case there there is the same [movieID] and [personID] number??? – Jonathan Nov 7 at 21:09
I'd have to know why this structure is advantageous to you in the first place to suggest a way to resolve collisions. Why not, for example, just have two separate lists: one by movieID and one by personID. – VoteyDisciple Nov 8 at 16:30

Thanks a lot for that !

link|flag

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.