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 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?