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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘