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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?