28

Are there any functions for recursively exploding and imploding multi-dimensional arrays in PHP?

4
  • 4
    I think you guys jumped the gun. Question you marked as answer is not exactly answer to this question. You should read before hitting that dupe button. It's not that hard. Commented May 22, 2015 at 1:08
  • 2
    This questions is incorrectly marked as a duplicate of implode-data-from-a-multi-dimensional-array. This question is about imploding / exploding in general while the other is about imploding a single key in a multi-dimensional array. Commented Mar 27, 2016 at 15:27
  • 1
    json_encode / json_decode are also good ways to pack, unpack and store multi-dimensional arrays. json_encode can encode arrays like ['foo'=>['bar'=>1, 'baz'=>[2,3]]] and will return a string like {"foo":{"bar":1,"baz":[2,3]}}. json_decode can then return the JSON-encoded string as an object. JSON data comes handy when passing data from PHP to Javascript as JSON (stdClass Object ( [foo] => stdClass Object ... }). Commented Mar 27, 2016 at 15:46
  • 1
    function multi_implode($glue,$array) { $out = ""; foreach ($array as $item) { if(is_array($item)){ if(empty($out)) $out=multi_implode($glue,$item); else $out.=$glue.multi_implode($glue,$item); }else{ if(empty($out)) $out=$item; else $out.=$glue.$item; } } return $out; } Commented Aug 15, 2016 at 6:07

4 Answers 4

52

You can do this by writing a recursive function:

function multi_implode($array, $glue) {
    $ret = '';

    foreach ($array as $item) {
        if (is_array($item)) {
            $ret .= multi_implode($item, $glue) . $glue;
        } else {
            $ret .= $item . $glue;
        }
    }

    $ret = substr($ret, 0, 0-strlen($glue));

    return $ret;
}

As for exploding, this is impossible unless you give some kind of formal structure to the string, in which case you are into the realm of serialisation, for which functions already exist: serialize, json_encode, http_build_query among others.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah! Serialize is what i didn't know i needed!
Any way to have it implode only certain columns inside the arrays arrays? like this, but print out only the db fields?
9

I've found that var_export is good if you need a readable string representation (exploding) of the multi-dimensional array without automatically printing the value like var_dump.

http://www.php.net/manual/en/function.var-export.php

1 Comment

2

You can use array_walk_recursive to call a given function on every value in the array recursively. How that function looks like depends on the actual data and what you’re trying to do.

Comments

2

I made two recursive functions to implode and explode. The result of multi_explode may not work as expected (the values are all stored at the same dimension level).

function multi_implode(array $glues, array $array){
    $out = "";
    $g = array_shift($glues);
    $c = count($array);
    $i = 0;
    foreach ($array as $val){
        if (is_array($val)){
            $out .= multi_implode($glues,$val);
        } else {
            $out .= (string)$val;
        }
        $i++;
        if ($i<$c){
            $out .= $g;
        }
    }
    return $out;
}
function multi_explode(array $delimiter,$string){
    $d = array_shift($delimiter);
    if ($d!=NULL){
        $tmp = explode($d,$string);
        foreach ($tmp as $key => $o){
            $out[$key] = multi_explode($delimiter,$o);
        }
    } else {
        return $string;
    }
    return $out;
}

To use them:

echo $s = multi_implode(
    array(';',',','-'),
    array(
        'a',
        array(10),
        array(10,20),
        array(
            10,
            array('s','t'),
            array('z','g')
        )
    )
);
$a= multi_explode(array(';',',','-'),$s);
var_export($a);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.