Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP json_编码{}个字符_Php_Arrays_Json - Fatal编程技术网

PHP json_编码{}个字符

PHP json_编码{}个字符,php,arrays,json,Php,Arrays,Json,我希望我的json_encode具有以下序列化字符串: ['Element', 'Density', { role: 'style' }] 我该怎么做 我试过了 $data = array('Element', 'Density', "{ role: 'style' }"); echo json_encode($data); 产生: ["Element","Density","{ role: 'style' }"] 请注意,您将其嵌套在另一个数组中的额外引号: $data =

我希望我的json_encode具有以下序列化字符串:

 ['Element', 'Density', { role: 'style' }]
我该怎么做

我试过了

   $data = array('Element', 'Density', "{ role: 'style' }");
   echo json_encode($data);
产生:

["Element","Density","{ role: 'style' }"]

请注意,您将其嵌套在另一个数组中的额外引号:

$data = array('Element', 'Density', array('role' => 'style'));
                                  // ^ another nesting here
echo json_encode($data);

您需要对数组键使用常规语法。
array('Element','Density',array('role'=>'style')。查看示例有时会有所帮助:您试图获取包含字典的数组,但插入的是字符串。请注意,当您将键与数字索引混合使用时,会得到以下结果:谢谢。它能起作用。我被甩了,因为嵌套数组使用{}而不是[]。@Yada是的,你真的不需要手工编写它,只需要构造一个普通的数组结构,
json_encode()
将自动处理
json_encode
默认情况下将PHP关联数组视为json对象,将数组键转换为属性。