PHP: Simple Method to Implode or Explode Arrays with Keys CodeUnit 21 NOV 2011

Associative arrays are very useful beasts, because values tied to keys makes it so much easier to build up pseudo objects in that it makes it easier to see which piece of array data refers to what.

There are a number of ways to store associative arrays of course, but this particular one below is probably one of the simplest for getting a single string out of a one level array with keys, and then transforming back into its array form when you need to make use of it again.

function array_implode_with_keys($array){
    $return = '';
    if (count($array) > 0){
    foreach ($array as $key=>$value){
    $return .= $key . '||||' . $value . '----';
    }
    $return = substr($return,0,strlen($return) - 4);
    }
    return $return;
}

function array_explode_with_keys($string){
    $return = array();
    $pieces = explode('----',$string);
    foreach($pieces as $piece){
        $keyval = explode('||||',$piece);
        if (count($keyval) > 1){
        $return[$keyval[0]] = $keyval[1];
        } else {
        $return[$keyval[0]] = '';
        }
    }
    return $return;
}

If you look at the code above, you will see that we are making use of delimiters here to keep firstly the keys aparts, and secondly the key from its associated value. In this case we are using —- to split the array elements, and |||| to split the key from its value.

In other words the resulting string is from the implode function is key1||||value1—-key2||||value2—-key3||||value3.

Obviously the explode function just works in reverse.

Related Posts:

About Craig Lotter

Software developer, husband and dad to two little girls. Writer behind An Exploring South African. I don't have time for myself any more.

  • Fx Nion

    Other way i use:

    function array_implode_with_keys($glue, $array){
        $return = array();
        if (count($array) > 0){
    foreach ($array as $key=>$value){
    $return[] = $key; $return[]=$value;
    }
    }
        return implode($glue, $return);
    }

  • Alex

    after 2 hours search on google, your function was the best :) Thanks

  • Maybe someone will find this useful:
    I had a long string of data that I had to turn into an associative array, looking like this

    Data1,Value1
    Data1,Value2
    Data2,Value3
    Data2,Value2
    Data1,Value2

    And so on, you get the gist of it. So I made the following adjustments in the function above provided:

    function array_explode_with_keys($string){
    $return = array();
    $pieces = explode(“\n”,$string);
    foreach($pieces as $piece){
    $keyval = explode(‘,’,$piece);
    if (count($keyval) > 1){
    if ( array_key_exists( $keyval[0], $return) ) {
    $return[$keyval[0]][]= $keyval[1];
    } else {
    $return[$keyval[0]] = array( $keyval[1]);
    }
    } else {
    $return[$keyval[0]] = ”;
    }
    }

    $buffer= array();
    foreach($return as $rowkey => $row) {
    $buffer[$rowkey]= array_unique($row);
    }
    $buffer= array_map(‘array_values’, $buffer);
    return $buffer;
    }

    “\n” being the separation between each line and ‘,’ the separation between each key and value. The buffer operation removes duplicate elements inside each array, and resets their keys. The resulting array looks like this:

    Array
    (
    [Data1] => Array
    (
    [0] => Value1
    [1] => Value2
    )

    [Data2] => Array
    (
    [0] => Value3
    [1] => Value2
    )

    )

    Hope this is useful for anyone who happens to stumble upon this site.