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

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

问题描述:

I have a problem with merging array's. I'll try array merge and combine but nothing is working.

First array

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
        )
    )

)

Second array

The second array is filled with room details, but the assigned users are not added yet.

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] => 
                )

        )

)

)        

I'll will put these arrays together to one array, so that the data is insert into the "rented_to" section. How can I get this array into an another array like this:

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
                    )

            )

    )

)                       

The code

$manager_add_new_room_rented_to is an array that contains the value of the first array in my example.

$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;             

Loop through $manager_add_new_room_rented_to[$room_type] and create the new array that you want.

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;