0

I have an array that is fetched from database using some bindModel in cakephp. finally now I want to change its format according to my liking to own. here is the array contains.

Array
(
    [0] => Array
        (
            [DriverLocation] => Array
                (
                    [id] => 1
                    [dispensary_id] => 1
                    [driver_id] => 85
                    [zip_code_id] => 42
                    [created] => 2015-05-25 12:01:14
                )

            [ZipCode] => Array
                (
                    [id] => 42
                    [province_id] => 3846
                    [city] => Rohtak
                    [zip_code] => 30215
                    [status] => active
                )

            [UserProfile] => Array
                (
                    [first_name] => Arman
                    [last_name] => Kumar
                )

        )

    [1] => Array
        (
            [DriverLocation] => Array
                (
                    [id] => 2
                    [dispensary_id] => 1
                    [driver_id] => 85
                    [zip_code_id] => 43
                    [created] => 2015-05-25 12:01:14
                )

            [ZipCode] => Array
                (
                    [id] => 43
                    [province_id] => 3846
                    [city] => Rohtak
                    [zip_code] => 15478
                    [status] => active
                )

            [UserProfile] => Array
                (
                    [first_name] => Arman
                    [last_name] => Kumar
                )

        )

    [2] => Array
        (
            [DriverLocation] => Array
                (
                    [id] => 3
                    [dispensary_id] => 1
                    [driver_id] => 77
                    [zip_code_id] => 41
                    [created] => 2015-05-25 12:45:47
                )

            [ZipCode] => Array
                (
                    [id] => 41
                    [province_id] => 3846
                    [city] => Malviya Vihar
                    [zip_code] => 12558
                    [status] => active
                )

            [UserProfile] => Array
                (
                    [first_name] => Manish
                    [last_name] => Kumar
                )

        )

)

I want to convert or change its format something like as follows. This will altered on the behalf of DriverLocation=>driver_id

Array
(
    [0] => Array
        (
            [DriverLocation] => Array
                (
                    [id] => 1
                    [dispensary_id] => 1
                    [driver_id] => 85
                    [zip_code_id] => 42
                    [created] => 2015-05-25 12:01:14
                )

            [ZipCode] => Array
                (
                   [0] =>  Array
                    (
                        [id] => 42
                        [province_id] => 3846
                        [city] => Rohtak
                        [zip_code] => 30215
                        [status] => active
                    )
                   [1] =>  Array
                    (
                        [id] => 43
                        [province_id] => 3846
                        [city] => Rohtak
                        [zip_code] => 15478
                        [status] => active
                    )

                )

            [UserProfile] => Array
                (
                    [first_name] => Arman
                    [last_name] => Kumar
                )

        )



    [1] => Array
        (
            [DriverLocation] => Array
                (
                    [id] => 3
                    [dispensary_id] => 1
                    [driver_id] => 77
                    [zip_code_id] => 41
                    [created] => 2015-05-25 12:45:47
                )

            [ZipCode] => Array
                (
                    [id] => 41
                    [province_id] => 3846
                    [city] => Malviya Vihar
                    [zip_code] => 12558
                    [status] => active
                )

            [UserProfile] => Array
                (
                    [first_name] => Manish
                    [last_name] => Kumar
                )

        )

)
1
  • 1
    Please be more descriptive! Commented May 25, 2015 at 14:01

2 Answers 2

1

You can use this loop:

foreach($ar as $k1=>$a1){
    foreach($ar as $k2=>$a2){
        if($k1 < $k2 && $a1["DriverLocation"]["driver_id"] == $a2["DriverLocation"]["driver_id"]){
            $ar[$k1]["ZipCode"][] = $a2["ZipCode"];
            if(isset($ar[$k1]["ZipCode"]["id"])){
                $ar[$k1]["ZipCode"][] = array("id"=>$ar[$k1]["ZipCode"]["id"]);
                unset($ar[$k1]["ZipCode"]["id"]);
            }
            unset($ar[$k2]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much dear :) its working according to my need.. :D thank you again.
0

Use array_push() function to make it

for sample example

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>

output

Array ( [0] => red [1] => green [2] => blue [3] => yellow )

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.