PHP数组问题,生成多行

时间:2015-05-25 13:41:26

标签: php arrays cakephp

我有一个数组,使用cakephp中的一些bindModel从数据库中获取。 最后,我想根据自己的喜好改变自己的格式。 这里是数组包含的。

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
                )

        )

)

我想转换或更改其格式,如下所示。 这将代表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
                )

        )

)

2 个答案:

答案 0 :(得分:1)

你可以使用这个循环:

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]);
        }
    }
}

答案 1 :(得分:0)

使用array_push()函数来实现它

示例

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

输出

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