【发布时间】:2017-10-05 12:07:11
【问题描述】:
在我的 php 中,我正在运行一个简单的查询,该查询从我拥有的数据库中返回一个结果集(0 个或多个)。
目前在前面的结果看起来像这样:
name: Smoothie description: Banana Smothie name: Phad Thai description: Noodles with shrimps name: Noodles description: Noodles with noodles.
字符串也可以是这样的,又名name: Smoothie description: Banana Smothie 或具有更多条目,如上例所示。
下面的代码给了我这个:
[{"name":"Smoothie","description":"Banana Smothie"}][{"name":"Phad Thai","description":"Noodles with shrimps"}]
我想要的是,所以它可以只是一个 json 对象:
[{"name":"Smoothie","description":"Banana Smothie"},{"name":"Phad Thai","description":"Noodles with shrimps"}]
这是我的 php:
<?php
include_once 'db/dbconnect.php';
$input = json_decode(stripcslashes($_POST['data']));
for ($i=0; $i < count($input); $i++) {
$stmt=$con->prepare("SELECT recipes.recipeName, recipes.recipeDescription FROM ingredients, recipes, recipesingredients WHERE recipes.recipeId = recipesingredients.recipeIdFK AND recipesingredients.ingredientIdFK = ingredients.IngredientId AND ingredients.ingredientName = ?");
$stmt->bind_param("s", $input[$i]);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($db_recipe_name, $db_recipe_description);
$rslt = array();
$arr = 0;
while ($stmt->fetch()) {
$rslt[$arr] = array('name' => $db_recipe_name, 'description' => $db_recipe_description);
$arr++;
}
$jsonRslt = json_encode($rslt);
echo $jsonRslt;
}
?>
有人可以帮我把它做成一个 json 对象吗?
【问题讨论】:
标签: php arrays json associative-array