douan4359 2019-02-04 11:47
浏览 302
已采纳

多维数组中的Array_slice? 但是输出不同

I have an array in php like this :

     $multid_array = array(
        "url" => "www.checkmyhouse.cpm",
        "date" => "2019/12/12",
        "minor" => 1,
        "sittingroom" => 1,
        "dinningroom" => 2,
        "toilet" => 2,
        "parking" => 1,
        "bedroom" => 2,
        "garage" => 2,
        "rooms" => 4,
        "garden" => 1,

        "url_1" => "-sameurl-",
        "date_1" => "2019/12/3",
        "minor_1" => 3,
        "sittingroom_1" => 2,
        "dinningroom_1" => 2,
        "toilet_1" => 3,
        "parking_1" => 1,
        "bedroom_1" => 2,
        "garage_1" => 6,
        "rooms_1" => 6,
        "garden_1" => 1,

        "url_2" => "-sameurl-",
        "date_2" => "2019/12/5",
        "minor_2" => 3,
        "sittingroom_2" => 2,
        "dinningroom_2" => 2,
        "toilet_2" => 3,
        "parking_2" => 1,
        "bedroom_2" => 5,
        "garage_2" => 6,
        "rooms_2" => 9,
        "garden_2" => 1,
    );

I have searched around, found this link. but not been able to figure it out. keep getting close. array_slice in multidimensional array?

This is the output I would like:

$array =
    [ 
       "levels" => array(
            "0" => array(
                "url" => "www.checkmyhouse.cpm",
                "date" => "2019/12/1",
                "minor" => 1,
                "sittingroom" => 1,
                "dinningroom" => 2,
                "toilet" => 2,
                "parking" => 1,
                "bedroom" => 2,
                "garage" => 2,
                "rooms" => 5,
                "garden" => 1,
            ),
            "1" => array(
                "url" => "-sameurl-",
                "date" => "2019/12/3",
                "minor" => 3,
                "sittingroom" => 2,
                "dinningroom" => 2,
                "toilet" => 3,
                "parking" => 1,
                "bedroom" => 2,
                "garage" => 6,
                "rooms" => 6,
                "garden" => 1,
            ),
            "2" => array(
                "url" => "-sameurl-",
                "date" => "2019/12/5",
                "minor" => 3,
                "sittingroom" => 2,
                "dinningroom" => 2,
                "toilet" => 3,
                "parking" => 1,
                "bedroom" => 5,
                "garage" => 6,
                "rooms" => 9,
                "garden" => 1,
            )
        )
    ];

Just trying to rearrange it. but I have used array_slice and could only get the output of the first 9 elements.

array_splice($multid_array,9);

But I want to run it automatically, so in case more information gets added into the array then it would still work

  • 写回答

1条回答 默认 最新

  • duandi6531 2019-02-04 12:05
    关注

    Why not use simple for loop: (in this case you can have various number of field in each array and they don't need to be fixed as when use array_chunk)

    $multid_array = array("url" => "www.checkmyhouse.cpm","date" => "2019/12/12","url_1" => "-sameurl-","date_1" => "2019/12/3","url_2" => "-sameurl-","date_2" => "2019/12/5",);
    
    $ans = [];
    foreach($multid_array as $k => $v) {
        $parts = explode("_", $k); 
        $pk = 0; //default as 0 as when no prefix set to key "0"
        if (count($parts) > 1) { // check if has suffix
            $pk = $parts[1];
            $k = $parts[0];
        }
        if (!isset($ans[$pk]))
            $ans[$pk] = []; // init array if first time
        $ans[$pk][$k] = $v;
    }
    

    Now $ans will contain your data - you can add "levels" key if you wish.

    This will only works if you have "_" saved to mark inner indexes and they are not used in the actual keys

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制