如何从 PHP 发送 JSON 格式的对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15618873/
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
How to send an array of objects in JSON format from PHP
提问by Kumari Manisha
New to php. I am trying to send JSON data to front end in name-value pair. I tried an example which I got hereThe following is my code fragment which sends the data in JSON name value format.
php 新手。我正在尝试将 JSON 数据以名称-值对的形式发送到前端。我尝试了一个我在这里得到的示例 以下是我的代码片段,它以 JSON 名称值格式发送数据。
while($stmt->fetch()){
$list = array('id' => $fid, 'name' => $fname);
}
$stmt->free_result();
$stmt->close();
echo json_encode($list);
I got this on front-end
我在前端得到了这个
Object {id: 12, name: "Manisha"}
The problem is I was expecting an array of objects. The above value is the last value obtained from the SQL query. What are the alterations I should make to this code so that I can get an array of objects. Something like
问题是我期待一组对象。上面的值是从 SQL 查询中获得的最后一个值。我应该对此代码进行哪些更改,以便我可以获得一组对象。就像是
[{"id":"1","name":"Kumari"}, {"id":"2","name":"KM"}, {"id":"3","name":"Manisha"}]
Please advice.
请指教。
回答by Jose Armesto
$list needs to be an array, and you can just push items to it like in this code:
$list 需要是一个数组,你可以像下面的代码一样向它推送项目:
$list = array();
while($stmt->fetch()){
$list[] = array('id' => $fid, 'name' => $fname);
}
$stmt->free_result();
$stmt->close();
echo json_encode($list);
You could also use the method fetch_all()to get all rows at once, instead of iterating with a loop. Although in this example you'd get all fields that you've selected, instead of just id and name.
您还可以使用fetch_all()方法一次获取所有行,而不是使用循环进行迭代。尽管在此示例中,您将获得您选择的所有字段,而不仅仅是 id 和 name。
$list = $stmt->fetch_all();
$stmt->free_result();
$stmt->close();
echo json_encode($list);
回答by Panagiotis Moustafellos
You should try pushing each object at the end of array like adding to a stack using array_push
(the equivalent of $array[] = $data
, but makes it more readable to you).
您应该尝试将每个对象推送到数组末尾,例如使用array_push
(相当于$array[] = $data
,但使其对您更具可读性)添加到堆栈中。
$list=array(); //instantiate the array
while($stmt->fetch()){
$data = new stdClass(); // create a new object
$data->id=$fid;
$data->name=$fname;
array_push($list,$data); // push object to stack array
}
$stmt->free_result();
$stmt->close();
echo json_encode($list);
回答by m79lkm
Your list array is only storing 1 row. Try this:
您的列表数组仅存储 1 行。尝试这个:
while ($stmt->fetch()) {
$list[] = array('id' => $fid, 'name' => $fname);
}
Hope this helps!
希望这可以帮助!
回答by Waygood
try creating an array of objects
尝试创建一个对象数组
$list = array();
while($stmt->fetch()) {
// create an object
$datum=new stdClass();
$datum->id=$fid;
$datum->name=$fname;
$list[] = $datum;
}
$stmt->free_result();
$stmt->close();
echo json_encode($list);
回答by atwright147
See if this works for you:
看看这是否适合你:
$n = 0;
while($stmt->fetch()){
$list[$n] = array('id' => $fid, 'name' => $fname);
$n++;
}
$stmt->free_result();
$stmt->close();
echo json_encode($list);
You were overwriting the $list
multiple times with an array.
你$list
用数组覆盖了多次。
回答by Shadoworker
Let's suppose we have this file structure without specific Objects inside :
假设我们有这个文件结构,里面没有特定的对象:
{
"Subjects" : []
}
//Get your Json file and decode it
$json = file_get_contents('CdStore/Pagetest.json');
$json_data = json_decode($json,true);
//Specifiy your Objects like this
$NewArray= array( 'Mathematics' => array(),'Physics' => array());
//Push the new Objects
array_push($json_data['Pagess'],$NewArray);
//Then encode to json
$array = json_encode($json_data);
//If you want to get the preview before saving
print_r($array);
//Save your file
file_put_contents('CdStore/Pagetest.json', json_encode($json_data));
As result you have :
结果你有:
{"Subjects":
[
{
"Mathematics":[],
"Physics":[]
}
]
}