【问题标题】:[PHP]Same Array into different Bracket[PHP]相同的数组放入不同的括号
【发布时间】:2017-08-01 22:57:11
【问题描述】:

在 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);
?>

【问题讨论】:

标签: php arrays


【解决方案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);
?>

【讨论】:

    【解决方案2】:

    您的代码应如下所示:

    <?php 
        $res1 = array();
        $result1 = mysqli_query($con,$dfcv) ;
        while($row2 = mysqli_fetch_array($result1)) {
            array_push($res1, $row2['username2']);
        }
        echo json_encode($res1);
    ?>
    

    当您执行array_push() 时,您在$row2 上添加了一个额外的数组

     array_push($res1, array($row2['username2']));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      相关资源
      最近更新 更多