PHP implode multi-dimensional array to string

Depending on your needs you could probably use json_encode 21 and json_decode 7 to achieve similar too, not sure if it might be a bit "lossy" but for arrays it should be ok.

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.

<?php        
$source_array = { [A] => ( [0] => "Apple", [1] => "Banana"), [B] => ( [0] => "Tiger", [1] => "Lion")}

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

Output: "Banana", "Lion"

Facebook Facebook Twitter Reddit

Leave a Reply