如何使用PHP为JSON创建数组?
从PHP代码中,我想创建一个json数组:
[
{"region":"valore","price":"valore2"},
{"region":"valore","price":"valore2"},
{"region":"valore","price":"valore2"}
]
我该怎么做?
从PHP代码中,我想创建一个json数组:
[
{"region":"valore","price":"valore2"},
{"region":"valore","price":"valore2"},
{"region":"valore","price":"valore2"}
]
我该怎么做?
易榨柠檬:http://www.php.net/manual/en/function.json-encode.php
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
在上述页面上有一个帖子,也可以处理复杂的嵌套数组(如果这是你的事情)。andyrusterholz at g-m-a-i-l dot c-o-m
使用 PHP 的本机json_encode
,如下所示:
<?php
$arr = array(
array(
"region" => "valore",
"price" => "valore2"
),
array(
"region" => "valore",
"price" => "valore2"
),
array(
"region" => "valore",
"price" => "valore2"
)
);
echo json_encode($arr);
?>
更新:在评论中回答您的问题。你这样做是这样的:
$named_array = array(
"nome_array" => array(
array(
"foo" => "bar"
),
array(
"foo" => "baz"
)
)
);
echo json_encode($named_array);