How to convert array to a string using methods other than JSON?

后端 未结 8 2102
臣服心动
臣服心动 2020-12-01 13:32

What is a function in PHP used to convert array to string, other than using JSON?

I know there is a function that directly does like JSON. I just don\'t remember.

相关标签:
8条回答
  • 2020-12-01 14:15

    You are looking for serialize(). Here is an example:

    $array = array('foo', 'bar');
    
    //Array to String
    $string = serialize($array);
    
    //String to array
    $array = unserialize($string);
    
    0 讨论(0)
  • 2020-12-01 14:25

    Display array in beautiful way:

    function arrayDisplay($input)
    {
        return implode(
            ', ',
            array_map(
                function ($v, $k) {
                    return sprintf("%s => '%s'", $k, $v);
                },
                $input,
                array_keys($input)
            )
        );
    }
    
    $arr = array('foo'=>'bar',
                  'baz'=>'boom',
                  'cow'=>'milk',
                  'php'=>'hypertext processor');
    
    echo arrayDisplay($arr);
    

    Displays:

    foo => 'bar', baz => 'boom', cow => 'milk', php => 'hypertext processor'
    
    0 讨论(0)
提交回复
热议问题