将json数组合并到json对象中

I have a problem joining a json array into a json object

I run a query to get detail about a selected part which returns only one result (expected). I then run another query to find any parts that are related to the first parent part. this may return none or multiple associated parts. I want my final output to look like this:

{
  "partCode": "P1",
  "description": "The Parent Part",
  "active": "1",
  "partType": "1",
  "partGroup": "BLR",
  "qty": "1",
  "page": "boiler",
  "relatedParts": [
       {
          "partCode": "P2",
          "description": "some related part",
          "active": "1",
          "partType": "1",
          "partGroup": "BLR",
          "qty": "1"
        },
        {
          "partCode": "P3",
          "description": "Another related part",
          "active": "1",
          "partType": "1",
          "partGroup": "BLR",
          "qty": "1"
        }
      ]
}

How do add the related parts[] array into the json{} object?

My queries are:

//parent part
$query= "SELECT * FROM `parts`
  INNER JOIN `partPrice` ON `parts`.`partCode` = `partPrice`.`partCode`
WHERE `parts`.`partCode` = '$part' AND `partPrice`.`active` = 1;";

$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

$json=array();
$relatedParts=array();

if (mysql_num_rows($result)>0){
    while($row=mysql_fetch_assoc($result)){ 
        $json[]=$row; 
        $json['relatedParts']=$relatedParts;
    }
}

//get MANADTORY related parts
$query= "SELECT parts.*
FROM
  partAssociation
  INNER JOIN parts ON partAssociation.childPart = parts.partCode
WHERE
  partAssociation.parentPart = '$part' AND
  partAssociation.association = 'Requires';";


$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

$json2=array();

if (mysql_num_rows($result)>0){
    while($row=mysql_fetch_assoc($result)){ 
        $json2[]=$row;

    }
}
$json['relatedParts']=$json2;

echo json_encode( $json);

EDIT: Found a solution probably not perfect but works; Getting the related records first then adding that result to the parent record Would still be interested in any slicker ways of doing this.

//get MANDATORY related parts
$query2 = "SELECT parts.*
FROM
  partAssociation
  INNER JOIN parts ON partAssociation.childPart = parts.partCode
WHERE
  partAssociation.parentPart = '$part' AND
  partAssociation.association = 'Requires';";

//set the holders 
$json=array();
$relatedParts=array();

//get any related records first
$resultRP = mysql_query($query2);
if (!$resultRP) {
  die("Invalid query: " . mysql_error());
}

if (mysql_num_rows($resultRP)>0){
    while($row=mysql_fetch_assoc($resultRP)){ 
        $relatedParts[]=$row;       
    }
}

//get parent record  
$result = mysql_query($query1);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

if (mysql_num_rows($result)>0){
    while($row=mysql_fetch_assoc($result)){ 
        $row['relatedParts']=$relatedParts;
        $json[]=$row;
    }
}


echo json_encode($json);
duanha3539
duanha3539 你能打印两个查询的输出吗?
接近 6 年之前 回复
dslpofp041310584
dslpofp041310584 它输出Object{0:Object,relatedParts:Array[2]},而我期待Object{0;Object}和relatedParts是第一个对象中的数组
接近 6 年之前 回复
doubeiji2602
doubeiji2602 如果你改变$json[]=$row怎么办?$JSON['relatedParts']=$relatedParts;to$row['relatedParts']=$relatedParts;$JSON[]=$行?
接近 6 年之前 回复
dongzhukuai8177
dongzhukuai8177 你当前的代码看起来应该工作;它出什么问题了?
接近 6 年之前 回复
drpiqlzrh62917192
drpiqlzrh62917192 请不要使用mysql_*函数,它们不再维护并且已被正式弃用。转而学习预备语句,并使用PDO或MySQLi。本文将帮助您做出决定。
接近 6 年之前 回复
douwu7563
douwu7563 你当前的输出是什么样的?
接近 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐