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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。