如何将php关联字符串数组的第一个“列”拆分为一个简单的字符串数组

时间:2022-04-19 22:07:05

I'm using this to to retrieve two columns in a database

我正在使用它来检索数据库中的两列

   while($comment[]=mysql_fetch_array($sql));

this returns an array with associative and number indices for each column. This works great but I would also like to create a new array from the orginial$comment[] that is just a simple array of strings (only the first column). What are my options? Is there any way to accomplish this without looping through a second time?

这将返回一个数组,其中包含每列的关联和数字索引。这很好但我也想从orginial $ comment []创建一个新数组,它只是一个简单的字符串数组(只有第一列)。我有什么选择?有没有办法在没有第二次循环的情况下实现这一目标?

2 个解决方案

#1


4  

Depending on how many columns you have, you could do something like:

根据您拥有的列数,您可以执行以下操作:

$result = mysql_query($sql);
while (list($col1[], $col2[]) = mysql_fetch_row($result));

$col1 will be an array of just column 1's values, and $col2 will be similar for column 2's values.

$ col1将是第1列值的数组,$ col2将与第2列的值类似。

#2


0  

$array = array();
for ($i = 0;$comment = mysql_fetch_array($sql);$i++){
    $array[$i] = $comment['field_name'];
}

#1


4  

Depending on how many columns you have, you could do something like:

根据您拥有的列数,您可以执行以下操作:

$result = mysql_query($sql);
while (list($col1[], $col2[]) = mysql_fetch_row($result));

$col1 will be an array of just column 1's values, and $col2 will be similar for column 2's values.

$ col1将是第1列值的数组,$ col2将与第2列的值类似。

#2


0  

$array = array();
for ($i = 0;$comment = mysql_fetch_array($sql);$i++){
    $array[$i] = $comment['field_name'];
}