Php 尝试在同一JSON对象内输出JSON值
现在,我的php文件输出JSON如下:Php 尝试在同一JSON对象内输出JSON值,php,json,Php,Json,现在,我的php文件输出JSON如下: $results = array(); //fetch all rows associated with the respective contact_id value //in review_shared table while ($row = $result2->fetch_assoc()) { //get the corresponding cat_id in the row $cat_id = $row["cat_id"]
$results = array();
//fetch all rows associated with the respective contact_id value
//in review_shared table
while ($row = $result2->fetch_assoc()) {
//get the corresponding cat_id in the row
$cat_id = $row["cat_id"];
//get the corresponding review_id in the row
$review_id = $row["review_id"];
//make an array called $results
$results[$row['cat_id']][] = $review_id;
}
$jsonData = array_map(function($catId) use ($results) {
return [
'category' => $catId,
'review_ids' => $results[$catId]
];
}, array_keys($results));
echo json_encode($jsonData);
[
{“类别”:142,“审查id”:92},
{“类别”:383,“审查id”:353},
{“类别”:203,“审查id”:149},
{“类别”:239,“审查id”:355},
{“类别”:239,“审查id”:201},
{“类别”:183,“审查id”:59},
{“类别”:183,“审查id”:62}
]
但我希望它输出如下:
$results = array();
//fetch all rows associated with the respective contact_id value
//in review_shared table
while ($row = $result2->fetch_assoc()) {
//get the corresponding cat_id in the row
$cat_id = $row["cat_id"];
//get the corresponding review_id in the row
$review_id = $row["review_id"];
//make an array called $results
$results[$row['cat_id']][] = $review_id;
}
$jsonData = array_map(function($catId) use ($results) {
return [
'category' => $catId,
'review_ids' => $results[$catId]
];
}, array_keys($results));
echo json_encode($jsonData);
[
{“类别”:142,“审查id”:92},
{“类别”:383,“审查id”:353},
{“类别”:203,“审查id”:149},
{“类别”:239,“审查id”:355,“审查id”:201},
{“类别”:183,“审查id”:59,“审查id”:62}
]
我希望在同一个对象中出现review\u id
,而不是重复category
编号。你能告诉我怎么做吗
这是我的密码:
$user\u id=“21”;
//在review_shared表中选择所有相关信息
//其中contact_id列等于$user_id。
//contact_id列中的值表示审阅与人员共享,$user_name,
//谁拥有那个号码,$user\u id
$sql=“从联系人id=?”处共享的审阅中选择*;
$stmt2=$con->prepare($sql)或die(mysqli_error($con));
$stmt2->bind_param('i',$user_id)或die(“MySQLi stmt绑定失败”。$stmt2->错误);
$stmt2->execute()或die(“MySQLi stmt execute失败”。$stmt2->错误);
$result2=$stmt2->get_result();
$results=array();
//获取与相应联系人id值关联的所有行
//审查中的共享表
而($row=$result2->fetch_assoc()){
//获取行中相应的cat_id
$cat_id=$row[“cat_id”];
//获取行中相应的审阅id
$review_id=$row[“review_id”];
//创建一个名为$results的数组
$results[]=数组(
//获取行中相应的cat_名称
'category'=>$row['cat_id'],
“review\u id”=>$review\u id,
);
}
echo json_编码($results);
我尝试的代码如下:
$review\u id\u results[]=array('review\u id'=>$review\u id);
但是我不知道如何正确操作,也不知道到底该做什么。在一个对象上不能有重复的属性。您真正想要的是将所有的
review\u id
列为一个数组。大概是这样的:
$results = array();
//fetch all rows associated with the respective contact_id value
//in review_shared table
while ($row = $result2->fetch_assoc()) {
//get the corresponding cat_id in the row
$cat_id = $row["cat_id"];
//get the corresponding review_id in the row
$review_id = $row["review_id"];
//make an array called $results
$results[$row['cat_id']][] = $review_id;
}
$jsonData = array_map(function($catId) use ($results) {
return [
'category' => $catId,
'review_ids' => $results[$catId]
];
}, array_keys($results));
echo json_encode($jsonData);
最终结果将是
[{"category":123,"review_ids":[1,2]},{"category":456,"review_ids":[3]}]
PHP和JSON都不允许对同一个键使用多个值的数组。您需要为想要的输出选择不同的格式。
{“category”:239,“review_id”:355,“review_id”:201},
您正在重新分配键review_id
。您的意思是输出应该像,{“category”:183,“review_id”:59,62}
?我将如何实现这一点?您真正想要的是{“category”:183,“review_id”:[355201]}