在我的php中,我正在运行一个简单的查询,它从我拥有的数据库中返回一个结果集(0或很多)。
目前正面朝下,rersult看起来像这样:
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对象吗?
答案 0 :(得分:1)
不是在循环内创建json_encode和echo的新数组,而是在循环之前创建一个并将每个对象添加到循环中。
示例:
<?php
include_once 'db/dbconnect.php';
$input = json_decode(stripcslashes($_POST['data']));
// Create an array before the loop
$json = [];
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);
while ($stmt->fetch()) {
// Add each element to the main array
$json[] = array('name' => $db_recipe_name, 'description' => $db_recipe_description);
}
}
// json_encode and echo the main array
echo json_encode($json);
?>
答案 1 :(得分:1)
在for
循环内,每次为$rslt
重新初始化i
数组,这就是为什么要获取多个JSON对象而不是单个JSON对象的原因。
您需要在$rslt
循环之外初始化for
并在for
循环后将其编码为JSON。
<?php
include_once 'db/dbconnect.php';
$input = json_decode(stripcslashes($_POST['data']));
// Initialize array here
$rslt = array();
$arr = 0;
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);
while ($stmt->fetch()) {
$rslt[$arr] = array('name' => $db_recipe_name, 'description' => $db_recipe_description);
$arr++;
}
}
// encode into JSON
$jsonRslt = json_encode($rslt);
echo $jsonRslt;
?>