douyun7718
2017-04-19 04:59
浏览 99

如何在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"
    }   
}

图片转代码服务由CSDN问答提供 功能建议

我有关于API JSON输出的问题。 我在循环中遇到JSON输出,当我强制将数组格式转换为索引数组时,它正在创建自己的索引。

PHP代码:

  $ sql =“SELECT name,fname,address FROM user_management”; 
 $ array = array(); 
 
 $ result = $ conn-> query($ sql)  ; 
 $ i = 1; 
 
if($ result-> num_rows> 0){
 // //每行的输出数据
 $ array = [“message”=>“success”];  
 while($ row = $ result-> fetch_assoc()){
 
 $ array [] = [“Hello”=>  $ row]; 
 
} 
 
} 
else 
 {
 echo“0 results”; 
} 
 // echo implode(',',$ array); 
 //取 输出数组用
echo json_encode($ array)粘贴它; 
 $ conn-> close(); 
?> 
   
 
 

我得到了 响应为:

  {
“message”:“success”,
“0”:{
“Hello”:{
“name”:“  harjot“,
”fname“:”tejinder“,
”地址“:”noida“
} 
},
”1“:{
”Hello“:{
”name“:” 遗憾“,
”fname“:”egegrregeger“,
”地址“:”gegreg“
} 
},
”2“:{
”Hello“:{
”name“:”  harjot1“,
”fname“:”harjot2“,
”地址“:”noida“
} 
},
”3“:{
”Hello“:{
”name“:”  har“,
”fname“:”har1“,
”地址“:”旁遮普“
} 
} 
} 
   
 
 

我想要 响应为没有数组自己创建的0,1,2,3。有没有办法访问数组的索引?

预期输出:< / strong>

  {“message”:“success”,
“Hello”:{
  “name”:“harjot”,
“fname”:“tejinder”,
“地址”:“noida”},
“Hello1”:{
“name”:“regret”,
“fname  “:”egegrregeger“,
”地址“:”gegreg“},
”Hello2“:{
”name“:”harjot1“,
”fname“:”harjot2“,
”地址“:  “noida”},
“Hello3”:{
“name”:“har”,
“fname”:“har1”,
“地址”:“旁遮普”
} 
} 
 <  / code>  
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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();
    ?>
    
    点赞 打赏 评论
  • doushi1964 2017-04-19 05:07

    Try this.

    $i = 0;
    while($row = $result->fetch_assoc()) {
    
        $array["Hello".$i++] = $row;
    
     } 
    

    OR

    $i = 0;
    while($row = $result->fetch_assoc()) {
    
        $array["Hello".$i] = $row;
        $i++;
     } 
    
    点赞 打赏 评论
  • douzhi4056 2017-04-19 05:08

    Try this

    if ($result->num_rows > 0) {
        // output data of each row
        $array=["message"=>"success"];
        $index = "";
        while($row = $result->fetch_assoc()) {
            $array["Hello" . $index] = $row;
            $index = $index ? ($index + 1) : 1;
        }
    } 
    
    点赞 打赏 评论

相关推荐