dongyi7513 2013-07-04 06:39 采纳率: 100%
浏览 6

拆分php数组[关闭]

i am using $_POST for getting all input values and it generates an array like:

[seen1] => 1
[flag1] => 0
[time1] => 275
[qtsnums1] => 17
[seen2] => 0
[flag2] => 0
[time2] => 0
[qtsnums2] => 11
[seen3] => 0
[flag3] => 0
[time3] => 0
[qtsnums3] => 9
[seen4] => 0
[flag4] => 0
[time4] => 0
[qtsnums4] => 5
[seen5] => 0
[flag5] => 0
[time5] => 0
[qtsnums5] => 10

i want to split:

[seen1] => 1
[flag1] => 0
[time1] => 275
[qtsnums1] => 17

and insert in 1 variable, how to do this?

  • 写回答

3条回答 默认 最新

  • dongwei5740 2013-07-04 06:44
    关注

    Assuming your input array is named $input, you can create your new array using this:

    $output = array();
    $wantedKeys = array('seen1', 'flag1', 'time1', 'qtsnums1');
    foreach ($wantedKeys as $key)
    {
        $output[$key] = $input[$key];
    }
    print_r($output);
    

    To get list containing first four keys, regardless how they are named:

    $wantedKeys = array_slice(array_keys($input), 0, 4);
    

    You can also use aforementioned array_chunk. If you want to get away with one-liner, you can do this:

    $output = current(array_chunk($input, 4, true));
    

    Note that it generates warning about references in newer versions of PHP. In such case you can use this:

    $output = array_chunk($input, 4, true)[0];
    

    This, however, will generate syntax error on older PHPs.

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入