doumu4032 2018-01-24 23:52
浏览 13
已采纳

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;             
  • 写回答

1条回答 默认 最新

  • douan7529 2018-01-25 00:35
    关注

    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;      
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号