douyun7718 2017-04-19 04:59
浏览 104
已采纳

如何在PHP中更改对象中JSON数组的输出?

I have an issue regarding the API JSON output. I am getting stuck into JSON output when in a loop, it is making its own index when i am forcing the array format to change into the index array.

PHP Code:

$sql = "SELECT name,fname,address FROM user_management";
$array = array();

$result = $conn->query($sql);
$i=1;

if ($result->num_rows > 0) {
    // output data of each row
    $array=["message"=>"success"];
    while($row = $result->fetch_assoc()) {

    $array[] = ["Hello" => $row];

 } 

} 
else 
{
    echo "0 results";
}
 //echo implode(',', $array);
 // Take output array glue it with the
echo json_encode($array);
$conn->close();
?>

I am getting the response as:

{
  "message": "success",
  "0": {
    "Hello": {
      "name": "harjot",
      "fname": "tejinder",
      "address": "noida"
    }
  },
  "1": {
    "Hello": {
      "name": "regret",
      "fname": "egegrregeger",
      "address": "gegreg"
    }
  },
  "2": {
    "Hello": {
      "name": "harjot1",
      "fname": "harjot2",
      "address": "noida"
    }
  },
  "3": {
    "Hello": {
      "name": "har",
      "fname": "har1",
      "address": "Punjab"
    }
  }
}

I want the response as without the 0,1,2,3 that array is making on its own.Is there a way to access the index of an arrays?

Expected output:

{   "message": "success",
    "Hello": {
      "name": "harjot",
      "fname": "tejinder",
      "address": "noida"   },
    "Hello1": {
      "name": "regret",
      "fname": "egegrregeger",
      "address": "gegreg"   },
    "Hello2": {
      "name": "harjot1",
      "fname": "harjot2",
      "address": "noida"   },
    "Hello3": {
      "name": "har",
      "fname": "har1",
      "address": "Punjab"
    }   
}
  • 写回答

3条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 香农解码的代码问题,无法输出解码结果
      • ¥15 Python操作注册表
      • ¥45 入门级别的一段VUE前端拍照像后端发送请求的代码,帮排错
      • ¥15 anaconda打开spyder后一直闪退,不知道怎么办
      • ¥15 解决迷宫问题中无法运行的问题
      • ¥15 关于aspnetcore中使用mqttnet库的entire
      • ¥15 关于#python#的问题,请各位专家解答!
      • ¥100 关于远控软件的两个问题
      • ¥15 基于STM32的AD8232心电采集装置设计
      • ¥15 我在wordpress里安装Ultimate menber 插件之后进行测试,点击注册之后发现网页打不了