如何使用php和json将数组值推送到另一个数组中

时间:2017-06-06 11:25:57

标签: php json

这里我从postadvertisement(表名)中选择了id,然后基于apon id有多少图像在那里我从图库中选择图像(表名)以及我在同一个数组中推送的所有图像,它正在工作很好,但我没有得到确切的JSON格式,如何使我的结果成为我的json格式

    $select = mysql_query("SELECT * FROM postadvertisement WHERE id = '$postadv' AND status != '1'");
    $count = mysql_num_rows($select);
    $farr = array();
    if($count > 0){
    while($fet= mysql_fetch_array($select)) {
      $row['PostId'] = $fet['id'];
      $row['title'] = $fet['title'];
      $row['subcatName'] = Get_subcatName($fet['subcat']);
      $row['minPrice'] = $fet['minPrice'];
      $row['maxPrice'] = $fet['maxPrice'];
      $row['location'] = Get_areaName($fet['location']);
      $row['description'] = $fet['description'];


    $farrs = array();
    $q1 = mysql_query("SELECT * FROM gallery WHERE `postadvId` = '$postadv' AND status != '1'");

    while($fq1 = mysql_fetch_array($q1))
    {
        $rowimg['PostId'] = $fet['id'];
        $rowimg['title'] = $fet['title'];
        $rowimg['imgId'] =$fq1['id'];
        $rowimg['images'] = "http://androidappfirst.com/b2b/app/images/".$fq1['images'];
    array_push($farrs,$rowimg); 
    }
      $row['multipleImages'] = $farrs;
      array_push($farr,$row);
    }

    echo json_encode(array("status"=>"success","productList"=>$farr));
 } 
 else
 {
    echo json_encode(array("status"=>"Error","response"=>"No Details found"));
 }
  

我收到回复

    {
  "status": "yes",
  "response": "success",
  "productList": [
    {
      "PostId": "2",
      "title": "Honda activa",
      "subcatName": "Commercial Land",
      "minPrice": "15000",
      "maxPrice": "0",
      "location": "Marathahalli",
      "description": "1 years  old",
      "multipleImages": [
        {
          "PostId": "2",
          "title": "Honda activa",
          "imgId": "1",
          "images": "http://androidappfirst.com/b2b/app/images/33584d27212ddcf24de5f91b3a0d241d.jpg"
        },
        {
          "PostId": "2",
          "title": "Honda activa",
          "imgId": "2",
          "images": "http://androidappfirst.com/b2b/app/images/763e710ee7c7ffdd96a41ca2a682c0a7.jpg"
        }
      ]
    }
  ]
}
  

我期待的JSON OUPUT

 "status": "yes",
"response": "success",
"Details":[
{
"categoryname": "Recently Added",
"categoriid":  "1",
"productList": [
{
    "PostId": "1",
    "title": "Honda activa",
    "subcatName": "Commercial Land",
    "minPrice": "15000",
    "maxPrice": "0",
    "location": "Marathahalli",
    "description": "1 years  old",
    "multipleImages": [
    {
      "PostId": "2",
      "title": "Honda activa",
      "imgId": "1",
      "images": "http://androidappfirst.com/b2b/app/images/33584d27212ddcf24de5f91b3a0d241d.jpg"
    },
    {
      "PostId": "2",
      "title": "Honda activa",
      "imgId": "2",
      "images": "http://androidappfirst.com/b2b/app/images/763e710ee7c7ffdd96a41ca2a682c0a7.jpg"
    }
  ]
}

]

}

]
}

在这里,我想在该数组中创建详细信息数组,我已推送 productList 数组值

1 个答案:

答案 0 :(得分:0)

由于目前尚不清楚您在哪里获取"categoryname""categoriid"值,我只想给您一个例子:

echo json_encode(array(
    "status"=>"success",
    "Details" => array(
        array(
            "categoryname" => $someVariable,
            "categoriid" => $anotherVariable,
            "productList"=>$farr,
        )
    )
));