Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何合并这两个JSON数组?_Php_Json - Fatal编程技术网

Php 如何合并这两个JSON数组?

Php 如何合并这两个JSON数组?,php,json,Php,Json,My 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]}

My 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]}]
我如何合并这些数组,使它们只是下面表单的一个数组。它不是简单地合并数组,而是可能将JSON对象中的一个键(
public\u review\u id
)的值转换为另一个键(
private\u review\u id
)。这是我想要的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\u review\u id
public\u review\u id
中,则它应该只出现在
private\u review\u id
键中

我试着使用
array\u unique
array\u 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);


?>

如果重构代码,可以立即将查询结果附加到所需的数据结构中,从而消除对数据进行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));

我在第43行的/var/www/html/CategorySearch.php中得到
解析错误:语法错误,意外的“?”
这是
$reviews[$cat\u id]['public\u review\u id']=$reviews[$cat\u id]['public\u review\u id']??[];当我删除额外的
时,我得到
解析错误:语法错误,意外“;”在第43行的/var/www/html/CategorySearch.php中
您没有使用PHP7+?看起来我有php版本5.5.9-1ubuntu4.21。我应该更新吗?但不是那么老,2017年2月。在任何情况下,我有一个问题得到的结果在变量$Puffic评论,让我看看更多……PHP 5.5是EOL在2016年7月,所以是的,你可能要考虑升级。至于变量,
$publicReviews
,为了清晰起见,我只是在回答中重命名了它们。您只需按正确的顺序获取查询结果,即可使
while
循环正常工作。我将更新我的答案,使其在没有空coalese运算符的情况下工作。Tx,JSON应该是一个数组,是吗?现在的输出是这样的:
{“383”:{“private_-review_-id”:[353],“public_-review_-id”:[]},“203”:{“private_-review_-id”:[149],“public_-review_-id”:[201],“public_-review_-id”:[]}
我希望它像:
[{“category”:“383”,“private_-review-id”:[353],“public_-review_-id”:[353],“public_-review_-id”:,“私人审查ID”:[149],“公共审查ID”:[]},“类别”:“239”,“私人审查ID”:[201],“公共审查ID”:[]}]