我的php生成两个JSON数组,如:
[{"category":183,"private_review_ids":[63,59,62]},
{"category":363,"private_review_ids":[331]},
{"category":371,"private_review_ids":[341]},
{"category":379,"private_review_ids":[350]}]
[{"category":363,"public_review_ids":[331]},
{"category":373,"public_review_ids":[343]},
{"category":384,"public_review_ids":[356]},
{"category":183,"public_review_ids":[347]}]
我如何合并这些数组,因此它们只是下面表格中的一个数组。它不仅仅是合并数组 - 它可以将值从一个键(public_review_ids
)转移到JSON对象中的另一个键(private_review_ids
)。这是我想要JSON数组的形式:
[{"category":183,"private_review_ids":[63,59,62],"public_review_ids":[347] },
{"category":363,"private_review_ids":[331],"public_review_ids":[]},
{"category":371,"private_review_ids":[341],"public_review_ids":[]},
{"category":379,"private_review_ids":[350]},"public_review_ids":[]},
{"category":373,"private_review_ids":[],"public_review_ids":[343]},
{"category":384,"private_review_ids":[],"public_review_ids":[356]}]
如您所见,如果值同时位于private_review_ids
和public_review_ids
,则它应仅显示在private_review_ids
键中。
我尝试使用array_unique
和array_merge
,但我确实没有成功。
这是我的代码:
<?php
require('myfile.php');
//here is the user_id, which is the corresponding user_id for username +5555555
$user_id = "21";
//Select all related info in the review_shared table
//where the contact_id column is equal to $user_id.
//a value in the contact_id column means a review is shared with a person, $user_name,
//who owns that number, $user_id
$sql = "SELECT * FROM review_shared WHERE contact_id = ?";
$stmt2 = $con->prepare($sql) or die(mysqli_error($con));
$stmt2->bind_param('i', $user_id) or die ("MySQLi-stmt binding failed ".$stmt2->error);
$stmt2->execute() or die ("MySQLi-stmt execute failed ".$stmt2->error);
$result2 = $stmt2->get_result();
//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,
'private_review_ids' => $results[$catId],
];
}, array_keys($results));
echo json_encode($jsonData);
//**********************
//select all rows where public_or_private column = 2
//in review table
$sql2 = "SELECT * FROM review WHERE public_or_private = 2";
$result2 = mysqli_query($con,$sql2);
//fetch all associated rows where public_or_private column = 2
while ($row = $result2->fetch_assoc()) {
//get the corresponding review_id in the row
$review2_id = $row["review_id"];
//get the corresponding cat_id in the row
$cat2_id = $row["cat_id"];
//make an array called $results
$results2[$row['cat_id']][] = $review2_id;
}
$jsonData2 = array_map(function($cat2Id) use ($results2) {
return [
'category' => $cat2Id,
'public_review_ids' => $results2[$cat2Id],
];
}, array_keys($results2));
echo json_encode($jsonData2);
?>
答案 0 :(得分:1)
如果您重构代码,可以通过立即将查询结果附加到所需的数据结构中,无需迭代数据4次(每次两次)。
如果您执行公开和私下审核查询,以便其结果位于变量$publicReviews
和$privateReviews
中,那么:
<?php
// Public and private review query results
$publicReviews = $stmt1->get_result();
$privateReviews = $stmt2->get_result();
// Prepare combined reviews array
$reviews = [];
// Iterate through private review results and append to combined reviews
while (($row = $privateReviews->fetch_assoc())) {
$category_id = $row['cat_id'];
$review_id = $row['review_id'];
$reviews[$category_id]['category'] = $category_id;
$reviews[$category_id]['private_review_ids'][] = $review_id;
$reviews[$category_id]['public_review_ids'] = [];
}
// Iterate through public review results and append to combined reviews
while (($row = $publicReviews->fetch_assoc())) {
$category_id = $row['cat_id'];
$review_id = $row['review_id'];
$reviews[$category_id]['category'] = $category_id;
// Create empty private reviews array, where it doesn't exist
if (! isset($reviews[$category_id]['private_review_ids'])) {
$reviews[$category_id]['private_review_ids'] = [];
}
// Add review id to public reviews where it doesn't exist in private reviews
if (! in_array($review_id, $reviews[$category_id]['private_review_ids'])) {
$reviews[$category_id]['public_review_ids'][] = $review_id;
}
}
echo json_encode(array_values($reviews));