在PHP中,我有一个我创建的数组,并且该数组的值就像['a','b','c'],但我不知道为什么,我的数组是这样的形式:[ [ 'A'],[ 'b'],[ 'C']] 这是我的代码:
<?php
$res1 = array();
$result1 = mysqli_query($con,$dfcv) ;
while($row2 = mysqli_fetch_array($result1)){
array_push($res1, array(
$row2['username2'])
);
}
echo json_encode($res1);
?>
答案 0 :(得分:1)
因为您将结果添加到其他数组(...array(
$row2['username2'])...
)。
<?php
$res1 = array();
$result1 = mysqli_query($con,$dfcv) ;
while($row2 = mysqli_fetch_array($result1))
array_push($res1, $row2['username2']);
echo json_encode($res1);
?>
答案 1 :(得分:1)
您的代码应如下所示:
<?php
$res1 = array();
$result1 = mysqli_query($con,$dfcv) ;
while($row2 = mysqli_fetch_array($result1)) {
array_push($res1, $row2['username2']);
}
echo json_encode($res1);
?>
当您执行$row2
array_push()
上添加了一个额外的数组
array_push($res1, array($row2['username2']));