简体   繁体   English

从 MySQL 列创建 PHP 数组

[英]Create PHP array from MySQL column

mysql_fetch_array will give me an array of a fetched row. mysql_fetch_array会给我一个获取行的数组。 What's the best way generate an array from the values of all rows in one column?从一列中所有行的值生成数组的最佳方法是什么?

you could loop through the array, and create a new one, like so:您可以遍历数组,然后创建一个新数组,如下所示:

$column = array();

while($row = mysql_fetch_array($info)){
    $column[] = $row[$key];
//Edited - added semicolon at the End of line.1st and 4th(prev) line

}

There is no function to do this using the mysql extension, you can do this:没有使用 mysql 扩展执行此操作的功能,您可以这样做:

$result = array();
while ($row = mysql_fetch_array($r, MYSQL_NUM)) {
    $result[] = $row[0];
}

It is apparently marginally faster to fetch the columns in a numerically indexed array, and there is no real benefit here to having the associative array format.获取数字索引数组中的列显然要快一些,并且使用关联数组格式在这里没有真正的好处。

Use a while loop to get the records and store them in an array:使用while循环获取记录并将它们存储在数组中:

$array = array();
while ($row = mysql_fetch_array()) {
    $array[] = $row['column-x'];
}

Loop through the result:循环遍历结果:

$result = mysql_query(...);
$data = array();
while ($row = mysql_fetch_array($result)) {
    array_push($data, $row["columnyouwant"]);
}
$result = mysql_query("SELECT columnname FROM table WHERE x=y");

$columnValues = Array();

while ( $row = mysql_fetch_assoc($result) ) {

  $columnValues[] = $row['columnname'];

}
// I'm using this for years and it works very good !! it's easy and logic
$translate = array();

while ($row = mysql_fetch_assoc($result))
  {
      $cullomnkey = $row['translshort']; // is what you want the key to be 
      $translate[$cullomnkey] = $row[$language]; // this is the key and the value in the array 
  }

If you use PDO instead of php-mysql the return value of PDO::query() implements the Traversable interface, meaning you can use it eg with foreach() (unlike the mysql result resource you get from mysql_query).如果您使用PDO而不是 php-mysql,则 PDO::query() 的返回值实现了 Traversable 接口,这意味着您可以将它与 foreach() 一起使用(与您从 mysql_query 获得的 mysql 结果资源不同)。

foreach( $pdo->query('SELECT x,y,z FROM foo') as $row ) {

And in case this does not suffice and you really, really need an array (ie get_class($x)==='Array'), there's the fetchAll() method.如果这还不够,而您确实需要一个数组(即 get_class($x)==='Array'),则可以使用fetchAll()方法。

If none of the above work, try:如果以上都不起作用,请尝试:

while( $row = mysqli_fetch_assoc( $result)){
     $array[] = $row['id'];
}

mysql_fetch_assoc is deprecated.

Many years later, just another way.许多年后,只是另一种方式。 I don't know if its performance is better or worse than the while loop method.不知道它的性能比while循环方法好还是差。

For values without commas yo can do:对于没有逗号的值,您可以执行以下操作:

$res = mysql_query("SELECT GROUP_CONCAT(colname) AS values FROM tablename");
$row = mysql_fetch_assoc($res);
$arrOfValues = explode(',', $row['values']);

For a generic solution, you may use a separator:对于通用解决方案,您可以使用分隔符:

$res = mysql_query("SELECT GROUP_CONCAT(colname SEPARATOR '##_sep_##') AS values FROM tablename");
$row = mysql_fetch_assoc($res);
$arrOfValues = explode('##_sep_##', $row['values']);
$query = mysql_query('SELECT * from yourTable');
function mysql_field_array( $query ) {
    $field = mysql_num_fields( $query );
    for ( $i = 0; $i < $field; $i++ ) {
        $names[] = mysql_field_name( $query, $i );
    }
    return $names;
}

$fields = mysql_field_array( $query );
$output = implode( ',', $fields );   //outputs the columns names
//echo count( $fields );  //this use if you want count of columns.
$columns = '{\"fields\":\".json_encode($output).\"}';
echo $columns; //for JSON output

you can do this :你可以这样做 :

        $columns = array();
        $i=1;
        while( $row = mysql_fetch_array($sql) )
           {
              $columns [$i]=$row['value'];
              $i++;
           }

If you don't need other information in that table, you can query for only the column you need and it makes it all more easy:如果您不需要该表中的其他信息,您可以只查询您需要的列,这样就更容易了:

$query = mysql_query("SELECT * FROM table WHERE id='$int' LIMIT 1");
$column = array();
$column  = mysql_fetch_array($query);

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

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