0

In PHP, I have an array which I created and usueally the value of that array are like ['a','b','c'], but I dont know why, my array is in this form : [['a'],['b'],['c']] Here is my code :

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

2 Answers2

1

Because you add the result into additional an array (...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);
?>
Neodan
  • 5,154
  • 2
  • 27
  • 38
1

Your code should look like this:

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

You've put an extra array on $row2 when you were doing the array_push()

 array_push($res1, array($row2['username2']));
giolliano sulit
  • 996
  • 1
  • 6
  • 11