php 如何使用 JSON 以外的方法将数组转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6817148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:21:09  来源:igfitidea点击:

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

phparraysstring-conversion

提问by maniclorn

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

PHP 中用于将数组转换为字符串的函数是什么,而不是使用 JSON?

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

我知道有一个直接类似于 JSON 的函数。我就是不记得了。

回答by Michael Berkowski

serialize()is the function you are looking for. It will return a string representation of its input array or object in a PHP-specific internal format. The string may be converted back to its original form with unserialize().

serialize()是您正在寻找的功能。它将以 PHP 特定的内部格式返回其输入数组或对象的字符串表示形式。可以使用 将字符串转换回其原始形式unserialize()

But beware, that not all objects are serializable, or some may be only partially serializable and unable to be completely restored with unserialize().

但请注意,并非所有对象都是可序列化的,或者某些对象可能只是部分可序列化而无法使用unserialize().

$array = array(1,2,3,'foo');
echo serialize($array);

// Prints
a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;s:3:"foo";}

回答by Tjekkles

Use the implode()function:

使用implode()函数:

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone

回答by T.Todua

readable output!

可读输出!

echo json_encode($array);     //outputs--->    "name1":"value1",  "name2":"value2",  ...

OR

或者

echo print_r($array, true);

回答by DevelRoot

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

您正在寻找serialize()。下面是一个例子:

$array = array('foo', 'bar');

//Array to String
$string = serialize($array);

//String to array
$array = unserialize($string);

回答by casivaagustin

Another good alternative is http_build_query

另一个不错的选择是 http_build_query

$data = array('foo'=>'bar',
              'baz'=>'boom',
              'cow'=>'milk',
              'php'=>'hypertext processor');

echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&');

Will print

会打印

foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&baz=boom&cow=milk&php=hypertext+processor

More info here http://php.net/manual/en/function.http-build-query.php

更多信息在这里http://php.net/manual/en/function.http-build-query.php

回答by umesh unnikrishnan

use php implode()or serialize()

使用 phpimplode()serialize()

回答by Somnath Muluk

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'

回答by BARIS KURT

There are different ways to do this some of them has given. implode(), join(), var_export(), print_r(), serialize(), json_encode()exc... You can also write your own function without these:

有不同的方法可以做到这一点,其中一些已经给出。 implode(), join(), var_export(), print_r(), serialize(), json_encode()exc...你也可以在没有这些的情况下编写自己的函数:

A For()loop is very useful. You can add your array's value to another variable like this:

一个For()循环是非常有用的。您可以将数组的值添加到另一个变量中,如下所示:

<?php
    $dizi=array('mother','father','child'); //This is array

    $sayi=count($dizi);
    for ($i=0; $i<$sayi; $i++) {
        $dizin.=("'$dizi[$i]',"); //Now it is string...
    }
         echo substr($dizin,0,-1); //Write it like string :D
?>

In this code we added $dizi's values and comma to $dizin:

在这段代码中,我们将 $dizi 的值和逗号添加到 $dizin 中:

$dizin.=("'$dizi[$i]',");

$dizin.=("'$dizi[$i]',");

Now

现在

$dizin = 'mother', 'father', 'child',

It's a string, but it has an extra comma :)

这是一个字符串,但它有一个额外的逗号:)

And then we deleted the last comma, substr($dizin, 0, -1);

然后我们删除了最后一个逗号, substr($dizin, 0, -1);

Output:

输出:

'mother','father','child'

“妈妈”、“爸爸”、“孩子”