PHP数组使用键合并到子数组

时间:2018-01-24 23:52:33

标签: php arrays merge array-merge

我在合并数组时遇到问题。我将尝试数组合并并合并但没有任何工作。

第一个数组

Array (
    [type1] => Array (
        [userid] => Array (
                [0] => 35
                [1] => 37
        )
        [from] => Array (
                [0] => 07-06-2017
                [1] => 09-06-2017
        )
        [till] => Array (
                [0] => 07-07-2017
                [1] => 09-07-2017
        )
    )

    [type3] => Array (
        [userid] => Array (
                [0] => 13
        )
        [from] => Array (
                [0] => 10-06-2017
        )
        [till] => Array (
                [0] => 10-07-2017
        )
    )

)

第二个数组

第二个数组充满了房间详细信息,但尚未添加已分配的用户。

Array (
[type1] => Array (
    [m2] => 
    [price] => 
    [rooms] => 1
    [extra] => 
    [rented_to] => Array
        (
            [0] => Array
                (
                    [userid] =>
                    [from] => 
                    [till] => 
                )

        )

)

[type3] => Array
(
    [m2] => 
    [price] => 
    [rooms] => 1
    [extra] => 
    [rented_to] => Array
        (
            [0] => Array
                (
                    [userid] => 
                    [from] => 
                    [till] => 
                )

        )

)

)        

我将这些数组放在一个数组中,以便将数据插入“rented_to”部分。如何将此数组放入另一个数组中:

Array (
    [type1] => Array (
        [m2] => 
        [price] => 
        [rooms] => 1
        [extra] => 
        [rented_to] => Array
            (
                [0] => Array
                    (
                        [userid] => 35
                        [from] => 07-06-2017
                        [till] => 07-07-2017
                    )
                [1] => Array
                    (
                        [userid] => 37
                        [from] => 09-06-2017
                        [till] => 09-07-2017
                    )

            )

    )

    [type3] => Array
    (
        [m2] => 
        [price] => 
        [rooms] => 1
        [extra] => 
        [rented_to] => Array
            (
                [0] => Array
                    (
                        [userid] => 13
                        [from] => 10-06-2017
                        [till] => 10-07-2017
                    )

            )

    )

)                       

代码

$ manager_add_new_room_rented_to是一个数组,其中包含我示例中第一个数组的值。

$manager_add_new_room_type_desc = $_POST['manager_add_new_room_type_desc'];
$manager_add_new_room_type_m2 = $_POST['manager_add_new_room_type_m2'];
$manager_add_new_room_type_rent_price = $_POST['manager_add_new_room_type_rent_price'];
$manager_add_new_room_type_rooms = $_POST['manager_add_new_room_type_rooms'];
$manager_add_new_room_type_extra = $_POST['manager_add_new_room_type_extra'];

$manager_add_new_room_rented_to = $_POST['manager_add_new_room_rented_to'];

$add_new_room_information = array();

foreach ( $manager_add_new_room_type_desc as $key => $room_type ) :

    $add_new_room_information[$room_type] = array( 
        'm2' => $manager_add_new_room_type_m2[$key], 
        'price' => $manager_add_new_room_type_rent_price[$key], 
        'rooms' => $manager_add_new_room_type_rooms[$key], 
        'extra' => $manager_add_new_room_type_extra[$key], 
        'rented_to' => array(
            array( 
                'userid' => '', 
                'from' => '', 
                'till' => '' 
            )
        ) 
    );

endforeach;             

1 个答案:

答案 0 :(得分:1)

循环createGaugeChart并创建所需的新数组。

$manager_add_new_room_rented_to[$room_type]