dsbifvcxr458755493 2018-01-23 00:39
浏览 74
已采纳

PHP生成不正确的json

I'm trying to get a json from mysql to use it for creating a tree view with help of bootstrap treeview ajax. But my php produces the wrong json.

I can't understand where do {"1": also "2" "6" and "7" comes from, since I don't have them in my table.

PHP

<?php
//fetch.php
$connect = mysqli_connect("hostname", "user", "", "dbname");
$query = "SELECT * FROM vtiger_customers";
$result = mysqli_query($connect, $query);
//$output = array();
while($row = mysqli_fetch_array($result))
{
  $sub_data["id"] = $row["customersid"];
  $sub_data["customer_name"] = $row["customer_name"];
  $sub_data["type"] = $row["type"];
if ($row["society"]=='')
  $sub_data["parent_id"] = "0";
else $sub_data["parent_id"] = $row["society"];
  $data[] = $sub_data;
}
foreach($data as $key => &$value)
{
$output[$value["id"]] = &$value;
}
foreach($data as $key => &$value)
{
if($value["parent_id"] && isset($output[$value["parent_id"]]))
  {
  $output[$value["parent_id"]]["nodes"][] = &$value;
  }
}
foreach($data as $key => &$value)
{
if($value["parent_id"] && isset($output[$value["parent_id"]]))
  {
  unset($data[$key]);
  }
}
echo json_encode($data);
?>   

JSON output that i get

  {"1":{"id":"78","customer_name":"Holding_1","type":"EPC","parent_id":"","nodes":    [{"id":"77","customer_name":"Ship owner","type":"Shipowner","parent_id":"78"}]},"2":{"id":"79","customer_name":"Holding_2","type":"EPC","parent_id":"","nodes":[{"id":"80","customer_name":"Ship owner 2","type":"Shipowner","parent_id":"79","nodes":[{"id":"82","customer_name":"Sub Ship owner","type":"Shipowner","parent_id":"80"}]},{"id":"81","customer_name":"Ship owner 1","type":"Shipowner","parent_id":"79"}]},"6":{"id":"83","customer_name":"Ship owner","type":"Shipowner","parent_id":""},"7":{"id":"84","customer_name":"Shipyard","type":"Shipyard","parent_id":""}}

JSON format which i need

[{"id":"78","customer_name":"Holding_1","type":"EPC","parent_id":"","nodes":    [{"id":"77","customer_name":"Ship owner","type":"Shipowner","parent_id":"78"}]},{"id":"79","customer_name":"Holding_2","type":"EPC","parent_id":"","nodes":[{"id":"80","customer_name":"Ship owner 2","type":"Shipowner","parent_id":"79","nodes":[{"id":"82","customer_name":"Sub Ship owner","type":"Shipowner","parent_id":"80"}]},{"id":"81","customer_name":"Ship owner 1","type":"Shipowner","parent_id":"79"}]},{"id":"83","customer_name":"Ship owner","type":"Shipowner","parent_id":""},{"id":"84","customer_name":"Shipyard","type":"Shipyard","parent_id":""}]

MySQL Table

customers_id  customers_number society   type       customer_name
    78          46546                    EPC          Holding_1 
    79          53456                    EPC          Holding_2 
    83          763645                   Shipowner    Ship owner 
    77          324543            78     Shipowner    Ship owner 
    84          63465435                 Shipyard     Shipyard 
    80          534523            79     Shipowner    Ship owner 
    81          836754            79     Shipowner    Ship owner 
    82          8746546           80     Shipowner    Sub Ship owner 
  • 写回答

2条回答 默认 最新

  • dongxidui1227 2018-01-23 03:14
    关注

    The initial $data array is an indexed array. To each key is assigned an item array. Each item array has the key parent_id and a value >= 0...

    ...The 1, 2, 6 and 7 are the indexes in the initial $data array, at which the value for the parent_id key of the corresponding item array is 0.

    In order to obtain what you need, you have to reset the array indexes of $data before printing it as json-encoded string:

    //...
    
    $data = array_values($data);
    
    echo json_encode($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面