在PHP中使用不同的定界符放大多维数组 [英] Implode multidimentional array with different delimiters in php

查看:91
本文介绍了在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));
                                }));

我也尝试过这样做:

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.

推荐答案

为什么不只在结构上使用 serialize(),然后再使用 unserialize()找回来?

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

这个内置的PHP绝对比您自己编写的任何自定义代码更好/更快/更安全。

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

这篇关于在PHP中使用不同的定界符放大多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆