Implode multidimentional array with different delimiters in php

扶醉桌前 提交于 2019-12-12 03:47:11

问题


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.


回答1:


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.




回答2:


I got this to work and figured I'd post the results for everyone who might need them in the future.

I realized that I know what the keys are and that I can use them in the implosion process. So I did this:

function Convert_From_Array($array)
{
    $Number = count($array['columns']);

    for ($i = 0 ; $i < $Number ; $i++)
    {            
        $array['columns'][$i]['search'] = implode('::',$array['columns'][$i]['search']);

        $array['columns'][$i] = implode('&&', $array['columns'][$i]);
    }
    $array['columns'] = implode('~~', $array['columns']);
    $array['order'][0] = implode('&&',$array['order'][0]);
    $array['order'] = implode('&&',$array['order']);
    $array['search'] = implode('&&',$array['search']);
    $array = implode('%%', $array);
}

Then to use this function I call it like this:

$PostKept = Convert_From_Array($KeepPost);

With $KeepPost being the multi-dimensional array I am trying to implode. The end results look like this:

1%%0&&Edit&&true&&true&&::false~~1&&&&true&&true&&::false~~2&&&&true&&true&&::false~~3&&&&true&&true&&::false~~4&&&&true&&true&&::false~~5&&&&true&&true&&::false~~6&&&&true&&true&&::false~~7&&&&true&&true&&::false~~8&&&&true&&true&&::false%%0&&asc%%0%%25%%&&false%%QDefs

Now I'll have to put it all back together later.



来源:https://stackoverflow.com/questions/41700528/implode-multidimentional-array-with-different-delimiters-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!