Cakephp how to use Set Class to make an assoc array?

Posted by michael on Stack Overflow See other posts from Stack Overflow or by michael
Published on 2010-04-07T09:58:44Z Indexed on 2010/04/07 10:03 UTC
Read the original article Hit count: 320

Filed under:

I have the output array from a $Model->find() query which also pulls data from a hasMany relationship:

Array(
[Parent] => Array
            (
                [id] => 1
            )

[Child] => Array
            (
                [0] => Array
                    (
                        [id] => aaa
                        [score] => 3
                        [src] => stage6/tn~4bbb38cc-0018-49bf-96a9-11a0f67883f5.jpg
                        [parent_id] => 1
                    )

                [1] => Array
                    (
                        [id] => bbb
                        [score] => 5
                        [src] => stage0/tn~4bbb38cc-00ac-4b25-b074-11a0f67883f5.jpg
                        [parent_id] => 1
                    )

                [2] => Array
                    (
                        [id] => ccc
                        [score] => 2
                        [src] => stage4/tn~4bbb38cc-01c8-44bd-b71d-11a0f67883f5.jpg
                        [parent_id] => 1
                    )

     )
)

I'd like to transform this output into something like this, where the child id is the key to additional child attributes:

Array(
[aaa] => Array
    (
        [score] => 3
        [src] => stage6/tn~4bbb38cc-0018-49bf-96a9-11a0f67883f5.jpg
    )
[bbb] => Array
    (
        [score] => 5
        [src] =>  stage0/tn~4bbb38cc-00ac-4b25-b074-11a0f67883f5.jpg
    )
[ccc] => Array
    (
        [score] => 2
        [src] => stage4/tn~4bbb38cc-01c8-44bd-b71d-11a0f67883f5.jpg
    )

}

Is there an easy way to use Set::extract, Set::combine, Set::insert, etc. to do this efficiently? I cannot figure it out.

© Stack Overflow or respective owner

Related posts about cakephp