samedi 2 avril 2016

how to add the key of an array to the array value in php ?

Hi I have an array and I have added the an id to the key of each array but I want that id to be added to the array value also.

The code which adds the keys and value to the array.

foreach ($data as $id => $name) {
            $arr[$id] = Category::where('parent_category_id', $id)->lists('id');
        }

now the array looks like this

Array
(
    [427] => Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => 277
                    [1] => 279
                    [2] => 426
                    [3] => 428
                    [4] => 429
                    [5] => 430
                    [6] => 431
                    [7] => 432
                    [8] => 433
                    [9] => 434
                )

        )

    [280] => Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => 281
                    [1] => 282
                    [2] => 435
                    [3] => 436
                    [4] => 437
                )

        )

    [283] => Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => 284
                    [1] => 285
                    [2] => 286
                )

        )

what I really want to achieve is that I want to add the key for example say the first key which is 427 to the array values so that I get all the ids. How would I be able to achieve this please assist.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire