简体   繁体   English

从数据库中获取逗号分隔值并存储在 PHP 中的数组中

[英]Getting comma separated values from database and storing in array in PHP

I'm trying to get fetch data from 'Seat' field in my database and get all values in an array.我正在尝试从数据库中的“座位”字段中获取数据并获取数组中的所有值。 The seats field in my database looks like this so far:到目前为止,我的数据库中的席位字段如下所示:

Seats座位
A1, A2 A1, A2
A3 A3

I want to turn these values into an array and print the array;我想把这些值变成一个数组并打印数组; but so far I've not had any luck但到目前为止我还没有运气

This is what I've tried so far:这是我迄今为止尝试过的:

$result = mysqli_query($conn, "SELECT Seats FROM flights_booked");

while($rows = mysqli_fetch_array($result)) {
 $seats=explode(',', $rows['Seats']);//what will do here
  print_r($seats);
  
}

My desired output is Array ( [0] => A1 [1] => A2 [2] => A3) when I print out the array - but so far I've got this:当我打印出数组时,我想要的 output 是Array ( [0] => A1 [1] => A2 [2] => A3) - 但到目前为止我已经得到了这个:

Array ( [0] => A1 [1] => A2 ) Array ( [0] => A3 )

Is there any way in which I can get all values in the same array?有什么方法可以获取同一个数组中的所有值吗?

Try using array_merge , like that:尝试使用array_merge ,如下所示:

$seats = [];
$result = mysqli_query($conn, "SELECT Seats FROM flights_booked");
while($rows = mysqli_fetch_array($result)) {
  $seats = array_merge($seats, explode(',', $rows['Seats']));
}
print_r($seats);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM