如何在一个数组PHP中设置子json数组
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在一个数组PHP中设置子json数组,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3105字,纯文字阅读大概需要5分钟。
内容图文
我是php的新手,我试图从以下响应中获取数据,但我想在子数组中设置此数据.这个怎么做?我有两个不同的表类别和产品.如何明智地显示多个产品类别.
提前致谢!
{
"data" : [
{
"id" : "1",
"recipe_name" : "Tofu Tikka",
"ingredients" : "Firm tofu 1 pack (bite sized cube)\r\n",
"prepration" : "Press tofu with the help of plate to remove moisture
and leave for 30-40 minutes, then cut in cubes.\r\n",
"category_id":"1",
"category_name":"Today's Menu"
}
]
}
如何像下面的方法一样在子数组中设置以上响应
{
"data":[
"category_id":"1",
"category_name":"Today's Menu"
"recipes::[
{
"id":"1",
"recipe_name":"Tofu Tikka",
"ingredients":"Firm tofu 1 pack ",
"prepration":"Press tofu with the help of plate"
}, {
"id":"2",
"recipe_name":"Tikka Paneer",
"ingredients":"Firm tofu 1 pack ",
"prepration":"Press tofu with the help of plate"
},
]
]
}
下面是我的PHP文件
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
// include database and object files
include_once '../config/database.php';
include_once '../objects/product.php';
// instantiate database and product object
$database = new Database();
$db = $database->getConnection();
// initialize object
$product = new Product($db);
// query products
$stmt = $product->read();
$num = $stmt->rowCount();
// check if more than 0 record found
if ($num>0) {
// products array
$products_arr=array();
$products_arr["data"]=array();
// retrieve our table contents
// fetch() is faster than fetchAll()
// https://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// extract row
// this will make $row['name'] to
// just $name only
extract($row);
$product_item=array(
"id" => $id,
"recipe_name" => $recipe_name,
"ingredients" => html_entity_decode($ingredients),
"prepration" => $prepration,
"category_id" => $category_id,
"category_name" => $category_name
);
array_push($products_arr["data"], $product_item);
}
echo json_encode($products_arr);
} else {
echo json_encode(
array("message" => "No products found.")
);
}
?>
解决方法:
我建议您在获取类别及其相关产品的记录时使用JOIN.它将需要单个查询和单个循环来生成所需的数组.这是您可以使用的示例查询.它将获得每个产品记录的类别名称,并且不会显示其中没有产品的那些类别.
SELECT * FROM categories AS c LEFT JOIN offers AS p ON c.category_id=p.category_id WHERE p.offer_id IS NOT NULL
注意:-不要在搜索查询中使用星号(*),而应使用表字段名称.
<?php
// initialize empty category array
$categoryArr = [];
// $row has product info with category id and name in it.
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
/* First key of categoryArr variable is category id. It will automatically create array key for each category.
* If an array key already exists, it will add prodcts in it.
*/
$categoryArr[$row['category_id']]['category_id'] = $row['category_id'];
$categoryArr[$row['category_id']]['category_name'] = $row['category_name'];
$categoryArr[$row['category_id']]['products'][] = $row;
}
/* Once loop done with its work. Need to reset array keys with the help of below function. */
$result = array_values($categoryArr);
echo json_encode($result); ?>
我没有测试过,它只是给您一个想法.希望您能改善它.
内容总结
以上是互联网集市为您收集整理的如何在一个数组PHP中设置子json数组全部内容,希望文章能够帮你解决如何在一个数组PHP中设置子json数组所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。