【问题标题】:json array with inner join带内连接的 json 数组
【发布时间】:2017-11-30 07:04:21
【问题描述】:

我希望我的 json 响应是这样的

{
subtotal="null",
create time ="2017-06-25 11:35:50",

products:[{product name:first,
          price:55}

]}

但我得到的是这个

[{subtotal="null", create time ="2017-06-25 11:35:50",

product name:first ,price:55},

]

这是我的 php 脚本

 <?php
require "connect.php";


$sql  ="SELECT * FROM products_orders JOIN products ON (products.PRODUCT_CODE =products_orders.PRODUCT_CODE) LEFT JOIN offers ON (offers.OFFER_ID = products_orders.OFFER_ID) ";


$result = mysqli_query($conn, $sql) or die("Error in READING " . mysqli_error($conn));
      $readsarray = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $readsarray[] = $row;
    }

    echo json_encode($readsarray, JSON_UNESCAPED_UNICODE);

    $conn->close();
?>

我是初学者,希望你能帮助我 提前致谢

【问题讨论】:

    标签: php mysql json inner-join


    【解决方案1】:

    请检查此答案。

    $data = array();
    $final_data = array();
    $products = array();
     while($row=mysqli_fetch_assoc($res)){
      $data['subtotal'] = $row['subtotal'];
      $data['create time'] = $row['create time'];
      $products['product name'] = $row['product name'];
      $products['price'] = $row['price'];
    
     $data['products'][] = $products;
    
     $final_data[] = $data;
    
    }
    
    echo json_encode($final_data, JSON_UNESCAPED_UNICODE);
    
    $conn->close();
    

    【讨论】:

      【解决方案2】:

      请检查此代码,此 json 数据存储所有订单详细信息。

       $data = array();
       $final_data = array();
       $products = array();
         while($row=mysqli_fetch_assoc($res)){
          $data['subtotal'] = $row['subtotal'];
          $data['create time'] = $row['create time'];
          $products['product name'] = $row['product name'];
          $products['price'] = $row['price'];
      
         $data['products'] = $products;
      
         $final_data[] = $data;
      
       }
      
      echo json_encode($final_data, JSON_UNESCAPED_UNICODE);
      
      $conn->close();
      

      【讨论】:

      • 感谢您的回答,我试过了,它使产品对象不是数组
      • 您想要产品数组吗?
      • 这个请检查我的新答案
      【解决方案3】:

      尝试这样定义$readsarray

      $i=0;
      while($row =mysqli_fetch_assoc($result))
      {
      $readsarray[$i]["subtotal"] = $row["subtotal"];
      $readsarray[$i]["create time"]=$row["create time"];
      $readsarray[$i]["products"]=array('product  name'=>$row["product name"],'price'=>$row["price"]);
      $i++;
      }
       echo json_encode($readsarray,true);
      
          $conn->close();
      ?>
      

      【讨论】:

      • {"PRODUCT_CODE":"15","CUSTOMERID":"2","CREATE_TIME":"2017-06-25 11:36:33","OFFER_ID":null,"SUBTOTAL ":null,"DONE":null,"VENDOR_ID":null,"products":{"NAME":"كلوركس مزيل بقع للملابس ","PRICE":"13.75","CATEGORY":"عناية بالمنزل","数量":"12"}}
      • 这个响应现在产品是对象而不是数组,它只显示响应中的第一个订单,为什么会这样?
      • 首先尝试此更改我更改代码以创建一个计数器,以便最终对象中包含的所有响应如果它与您一起使用我将解决产品的人民
      • 计数器现在工作正常,我该如何解决我想让产品成为对象数组的其他问题
      • $readsarray[$i]["products"][]=array('NAME'=>$row["NAME"],'PRICE'=>$row["PRICE"], 'CATEGORY'=>$row["CATEGORY"],'QUANTITY'=>$row["QUANTITY"]);我做到了,现在可以正常工作了,非常感谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 2012-09-07
      • 2015-03-19
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多