Multidimensional Array to String

后端 未结 7 631
挽巷
挽巷 2020-12-03 11:59

I am trying to convert a multidimensional array into a string with a particular format.

function convert_multi_array($array) {
    foreach($array as $value)          


        
7条回答
  •  悲&欢浪女
    2020-12-03 12:24

    You are overwriting $array, which contains the original array. But in a foreach a copy of $array is being worked on, so you are basically just assigning a new variable.

    What you should do is iterate through the child arrays and "convert" them to strings, then implode the result.

    function convert_multi_array($array) {
      $out = implode("&",array_map(function($a) {return implode("~",$a);},$array));
      print_r($out);
    }
    

提交回复
热议问题