Kumar Akarsh
Kumar Akarsh

Reputation: 4980

Cakephp Set::combine. Transforming an array

I have to convert the first array into the second, in the form of array(id=> data). I am doing

Set::combine($array, '{n}.{n}.id', '{n}.{n}');

But its not working.Please tell me what is wrong in this or how it should be done.

Array1:

 Array
    (
        [0] => Array
            (
               [0] => Array
                    (
                        [id] => 1
                        [user_id] => 1
                        [group_id] => 7
                        [comment] => Comment 1.
                    )
               [1] => Array
                (
                    [id] => 3
                    [user_id] => 4
                    [group_id] => 8
                    [comment] => Comment 4
                )

        )

Array2:

Array(
                   [1] => Array
                        (
                            [id] => 1
                            [user_id] => 1
                            [group_id] => 7
                            [comment] => Comment 1.
                        )
                   [3] => Array
                    (
                        [id] => 3
                        [user_id] => 4
                        [group_id] => 8
                        [comment] => Comment 4
                    )
        )

Upvotes: 0

Views: 3044

Answers (1)

Ben Wilson
Ben Wilson

Reputation: 544

Because Set::combine() uses Set::extract() heavily, I don't believe it's possible to use more than a single numeric dimension at this time without some workarounds.

Set::combine( $array[0], '{n}.id', '{n}' ); will work.

Use the following if you have multiple dimensions to cycle through:

$combined = array();
foreach ( $array as $val) {
  $combined = array_merge( $combined, $val );
}
$combined = Set::combine( $combined, '{n}.id', '{n}' );

Upvotes: 4

Related Questions