My php generates two JSON arrays like:
[{"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]}]
How could I merge these arrays so they are just one array of the form below. It is not simply merging the arrays - it is the possible tranferring of the value from one key (public_review_ids
) to another key (private_review_ids
) within the JSON Object. Here's the form I want the JSON Array:
[{"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]}]
As you can see, if the value is in both private_review_ids
and public_review_ids
, it should appear in just the private_review_ids
key.
I tried using array_unique
and array_merge
but I was having no success, really.
Here's my code:
<?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);
?>