开发者

how to split off first "column" of a php associative array of strings into a simple array of strings

开发者 https://www.devze.com 2023-04-03 09:01 出处:网络
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.

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 stri开发者_运维问答ngs (only the first column). What are my options? Is there any way to accomplish this without looping through a second time?


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消