0

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

1 Answer 1

4

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}' );
Sign up to request clarification or add additional context in comments.

1 Comment

this is exactly what I have been looking for ever since. :)

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.