Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中更改对象中JSON数组的输出?_Php_Arrays_Json_Api - Fatal编程技术网

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

如何在PHP中更改对象中JSON数组的输出?,php,arrays,json,api,Php,Arrays,Json,Api,我有一个关于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) { // output data of each row

我有一个关于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) {
    // 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();
?>
我得到的答复如下:

{
  "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"
    }
  }
}
我希望响应没有数组自己生成的0,1,2,3。是否有方法访问数组的索引

预期输出:

{   "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"
    }   
}
$array[] = ["Hello" => $row];
$array["Hello".$i] = $row;
<?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();
?>

您有维护计数器的
$i
声明,但您没有使用它

更改此选项:

{   "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"
    }   
}
$array[] = ["Hello" => $row];
$array["Hello".$i] = $row;
<?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();
?>
这个:

{   "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"
    }   
}
$array[] = ["Hello" => $row];
$array["Hello".$i] = $row;
<?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();
?>
同时添加
$i++用于维护和增加计数器值

PHP代码:

{   "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"
    }   
}
$array[] = ["Hello" => $row];
$array["Hello".$i] = $row;
<?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();
?>

您有维护计数器的
$i
声明,但您没有使用它

更改此选项:

{   "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"
    }   
}
$array[] = ["Hello" => $row];
$array["Hello".$i] = $row;
<?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();
?>
这个:

{   "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"
    }   
}
$array[] = ["Hello" => $row];
$array["Hello".$i] = $row;
<?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();
?>
同时添加
$i++用于维护和增加计数器值

PHP代码:

{   "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"
    }   
}
$array[] = ["Hello" => $row];
$array["Hello".$i] = $row;
<?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();
?>

试试这个

$i = 0;
while($row = $result->fetch_assoc()) {

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

 } 

试试这个

$i = 0;
while($row = $result->fetch_assoc()) {

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

 } 

试试这个

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;
    }
} 
试试这个

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;
    }
} 

$array[]=[“Hello”=>$row]更改
$array[“Hello”。$i]=$row输出已更改,但仍将获得具有0,1,2,3键的数组自己的索引。如何删除它们?在屏幕截图中显示您的代码。从
$array[]=[“Hello”=>$row]更改
$array[“Hello”。$i]=$row输出已更改,但仍将获得具有0,1,2,3键的数组自己的索引。如何删除它们?在屏幕截图中显示您的代码。谢谢,老兄,又一次棒极了!谢谢,老兄,又一次棒极了!