PHP 数组合并到带键的子数组

PHP array merge to subarray with keys

我在合并数组时遇到问题。我会尝试数组合并和组合,但没有任何效果。

第一个数组

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;             

遍历 $manager_add_new_room_rented_to[$room_type] 并创建所需的新数组。

foreach ( $manager_add_new_room_type_desc as $key => $room_type ) :
    $renters = $manager_add_new_room_rented_to[$room_type];
    $rented_to = array();
    foreach ($renters['userid'] as $index => $userid) :
        $rented_to[] = array('userid' => $userid, 'from' => $renters['from'][$index], 'to' => $renters['to'][$index]);
    endforeach;

    $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' => $rented_to
        ) 
    );
endforeach;