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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料