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条回答 默认 最新

  • douzong3599 2017-04-19 05:05
    关注

    You was having declaration of $i for maintaining counter but you were not using it.

    Change this:

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

    This:

    $array["Hello".$i] = $row;
    

    Also add $i++; for maintaining and increasing counter value.

    PHP code:

    <?php //
    $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".$i] = $row;//added line here
        $i++;//added this line to increment counter everytime.
     } 
    
    } 
    else 
    {
        echo "0 results";
    }
     //echo implode(',', $array);
     // Take output array glue it with the
    echo json_encode($array);
    $conn->close();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题