0

I have this array:

Array
(
    [0] => Array
        (
            [name] => Ben
            [matchedMovie] => Array
                (
                    [name] => Saw
                    [genre] => Horror
                    [patheMovie] => Texas Chainsaw 3D
                    [patheMovieGenre] => Horror
                    [score] => 100.00
                )

        )

    [1] => Array
        (
            [name] => Ben 
            [matchedMovie] => Array
                (
                    [name] => Shooter
                    [genre] => Action, Thriller
                    [patheMovie] => The Shining
                    [patheMovieGenre] => Horror, Suspense/Thriller 
                    [score] => 52.38
                )

        )

    [2] => Array
        (
            [name] => Dick
            [matchedMovie] => Array
                (
                    [name] => Resident Evil Movie
                    [genre] => Action/Horror
                    [patheMovie] => Texas Chainsaw 3D
                    [patheMovieGenre] => Horror
                    [score] => 63.16
                )

        )
)

I want to sort it something like this (don't know if i got it exactly right):

Array
    (
        [0] => Array
            (
                [name] => Ben
                [matchedMovie][0] => Array
                    (
                        [name] => Saw
                        [genre] => Horror
                        [patheMovie] => Texas Chainsaw 3D
                        [patheMovieGenre] => Horror
                        [score] => 100.00
                    )

                [matchedMovie][1] => Array
                    (
                        [name] => Shooter
                        [genre] => Action, Thriller
                        [patheMovie] => The Shining
                        [patheMovieGenre] => Horror, Suspense/Thriller 
                        [score] => 52.38
                    )

            )

        [1] => Array
            (
                [name] => Dick
                [matchedMovie][0] => Array
                    (
                        [name] => Resident Evil Movie
                        [genre] => Action/Horror
                        [patheMovie] => Texas Chainsaw 3D
                        [patheMovieGenre] => Horror
                        [score] => 63.16
                    ) 
            )
    )

So that the matchedMovie arrays are under the same name. How should i do this?

I have tried this function:

function group_by_key($array) {
        $result = array();
        foreach ($array as $sub) {
            foreach ($sub as $k => $v) {
                $result[$k][] = $v;
            }
        }
        return $result;
    }

But that doesn't work.

4
  • 1
    So you mention sort in your title, but not in your question. Is there actually some sort of sort you are trying to do as well? Commented Dec 7, 2012 at 18:03
  • I'm a bit confused about the difference between the two arrays. Perhaps you could outline in words what you are trying to achieve. Commented Dec 7, 2012 at 18:04
  • sorry, my english is not the best. I will try to explain it. As you can see in the first multidimensional array that there is a name twice mentioned. I want to merge the matchedMovie of that second name with the first. So that its all under the same name and every name is unique. Commented Dec 7, 2012 at 18:07
  • you would need to do the comparison if to see if they have the same 'name' value. unless you are using the value of 'name' as the key, then you can directly set them to the same key. Commented Dec 7, 2012 at 18:08

1 Answer 1

1

Just a quick stab at it.

function group_by_key($array){
    $result = array();
    foreach($array as $row){
        if(!isset($result[$row['name']]){
            $result[$row['name']] = array(
                'name'=>$row['name'],
                'matchedMovie'=>array($row['matchedMovie'])
            );
        } else {
            $result[$row['name']]['matchedMovie'][] = $row['matchedMovie'];
        }
    }
    return array_values($result);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.