将一列表转换为数组

I read most of the posts related to my question but none can resolve my simple issue.

I am using PHP and MySQLi to retrieve data from my server.

In my Database I have a one-dimensional table (i.e. one column only) and I want to put its data inside an array.

Until now I was using JSON to hold the data from my server.this is my code:

<?php
include 'ChromePhp.php';
$con=mysqli_connect("197.168.240.100","jsoncloud","nuahyu,hs","json");
// Check connection
if (mysqli_connect_errno())
  {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();
ChromePhp::warn('Failed to connect to MySQL');

  }

$sql="SELECT `street` FROM  `users` ";

$result=mysqli_query($con,$sql);

$rows = array();
while ($row = mysqli_fetch_all($result,MYSQLI_ASSOC)) {
    $rows[] = $row[0];

}
echo json_encode($rows);


// Free result set
mysqli_free_result($result);

mysqli_close($con);
?>

My final goal is to echo the array back so I can use it with Ajax in another JS file

in the other JS file I have the following code:

$.ajax({
  type: 'POST',
  url: 'js/SQLusersstreets.php',
  dataType: 'json',
  success: function(jsonData) {
console.log(jsonData);
  },
  error: function() {
    alert('Error loading SQLusersstreets.php from GoogleMapsCode.js');
  }
});

This is my output when I open the browser's debugger:

Output

I am trying to return a simple array, what am I doing wrong?

Change mysqli for PDO

Assuming a connection is already established, you will need only one line of code:

echo json_encode($pdo->query("SELECT `street` FROM `users`")->fetchAll(PDO::FETCH_COLUMN));

but if you prefer to write a lot of code, then change fetch_all to fetch_assoc

try this, if this is what you are asking about:

for($i=0;$i<sizeof($rows);$i++){

     echo $rows[$i]['id']; //for example
}

mysqli_fetch_all is a shortcut for while($row = mysqli_fetch) {}, so you can skip the while cycle.

$sql="SELECT DISTINCT `street` FROM  `users` ";

$result=mysqli_query($con,$sql);

$rows = mysqli_fetch_all($result,MYSQLI_ASSOC);
echo json_encode(array_map(function($i) { return $i['street']; }, $rows));

See the documentation here for more examples http://php.net/manual/en/mysqli-result.fetch-all.php