数组或Json转换为字符串

时间:2019-04-24 07:31:42

标签: php arrays json

将字符串更改为数组是

$string = '[{"name":"jack","address":"who knows"},{"name":"jill","address":"who knows too"}]';

$array = json_decode($array,true);

但是如果反向将数组更改为字符串

$array = [{"name":"jack","address":"who knows"},{"name":"jill","address":"who knows too"}];

$string = $array; // $string is $string like on first example

1 个答案:

答案 0 :(得分:0)

您可以将json_encode用作:

$string = '[{"name":"jack","address":"who knows"},{"name":"jill","address":"who knows too"}]';
$array = json_decode($string,true); // convert string -> array
$string = json_encode($array); // convert array -> string 
echo $string; // will output the original string