Search code examples
phparraysmultidimensional-arrayimplode

Implode multidimentional array with different delimiters in php


I have a multi-dimensional array that I would like to implode (and then later be able to explode back into the original multi-dimensional array). Is there a way to implode, keeping the keys?

Here's an example of what my array looks like:

Array ( 
    [draw] => 1 
    [columns] => Array ( 
        [0] => Array ( 
            [data] => 0 
            [name] => Edit 
            [searchable] => true 
            [orderable] => true 
            [search] => Array ( 
                [value] => 
                [regex] => false ) )
        [1] => Array ( 
            [data] => 1 
            [name] => 
            [searchable] => true 
            [orderable] => true 
            [search] => Array ( 
                [value] => 
                [regex] => false ) ) 
        [2] => Array ( 
            [data] => 2 
            [name] => 
            [searchable] => true 
            [orderable] => true 
            [search] => Array ( 
                [value] => 
                [regex] => false ) ) 

Here's what I've tried without success:

$out = implode('::',array_map(function($a)
                                {
                                    return implode('&&',array_map(function($b)
                                                                    {
                                                                        return implode('~~',$b);
                                                                    },$array));
                                }));

I've also tried this:

foreach($array as $Value)
{
    if(is_array($Value))
    {
        foreach($Value as $Columns)
        {
            if(is_array($Columns))
            {
                foreach($Columns as $Search)
                {
                    if(is_array($Search))
                    {
                        $Search = implode("::",$Search);
                    }
                    //echo "<br>Search: "; print_r($Search);
                }
            }
            else
            {
                echo "<br>Columns: "; print_r($Columns);
                //$Columns = implode("&&",$Columns);
            }
        }
    }
    else
    {
        //$Value = implode("~~",$Value);
    }
}
//print_r($array);

What I would like for it to look like at the end of the implode is:

[draw] => 1 :: [columns] => &&  [0] => ~~ [data] => 0 ~~ [name] => Edit ~~ [searchable] => true ~~ [orderable] => true ~~ [search] => %% [value] => %% [regex] => false && [1] => ~~ [data] => 1 ~~ [name] => ~~ [searchable] => true ~~ [orderable] => true ~~ [search] => %% [value] => %% [regex] => false

At least I'm pretty sure I've got all the delimiters in the right place. If I can't keep the keys that's okay as long as the delimiters are in the correct place and I can recreate the multi-dimensional array later.


Solution

  • Why don't you just use serialize() on the structure and then unserialize() to get it back?

    This PHP builtin will definitely work better/faster/safer than any custom code you wrote yourself.