How to “flatten” a multi-dimensional array to simple one in PHP?

前端 未结 23 2502
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 01:03

It\'s probably beginner question but I\'m going through documentation for longer time already and I can\'t find any solution. I thought I could use implode for each dimensio

23条回答
  •  执笔经年
    2020-11-22 01:16

    I found a simple way to convert multilevel array into one. I use the function "http_build_query" which converts the array into a url string. Then, split the string with explode and decode the value.

    Here is a sample.

    $converted = http_build_query($data);
    $rows = explode('&', $converted);
    $output = array();
    foreach($rows AS $k => $v){
       list($kk, $vv) = explode('=', $v);
       $output[ urldecode($kk) ] =  urldecode($vv);
    }
    return $output;
    

提交回复
热议问题