【问题标题】:Create PHP array from MySQL column从 MySQL 列创建 PHP 数组
【发布时间】:2010-12-05 00:36:16
【问题描述】:

mysql_fetch_array 会给我一个获取行的数组。从一列中所有行的值生成数组的最佳方法是什么?

【问题讨论】:

    标签: php mysql arrays


    【解决方案1】:

    你可以遍历数组,然后创建一个新数组,如下所示:

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

    【讨论】:

      【解决方案2】:

      没有使用mysql扩展的功能,你可以这样做:

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

      获取数字索引数组中的列显然稍微快一些,并且在这里使用关联数组格式并没有真正的好处。

      【讨论】:

        【解决方案3】:

        使用while 循环获取记录并将它们存储在数组中:

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

        【讨论】:

          【解决方案4】:

          遍历结果:

          $result = mysql_query(...);
          $data = array();
          while ($row = mysql_fetch_array($result)) {
              array_push($data, $row["columnyouwant"]);
          }
          

          【讨论】:

            【解决方案5】:
            $result = mysql_query("SELECT columnname FROM table WHERE x=y");
            
            $columnValues = Array();
            
            while ( $row = mysql_fetch_assoc($result) ) {
            
              $columnValues[] = $row['columnname'];
            
            }
            

            【讨论】:

              【解决方案6】:
              // 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 
                }
              

              【讨论】:

                【解决方案7】:

                如果你使用PDO 而不是 php-mysql,PDO::query() 的返回值实现了 Traversable 接口,这意味着你可以使用它,例如使用 foreach() (与您从 mysql_query 获得的 mysql 结果资源不同)。

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

                如果这还不够,而您真的非常需要一个数组(即 get_class($x)==='Array'),则可以使用 fetchAll() 方法。

                【讨论】:

                  【解决方案8】:

                  如果以上都不起作用,请尝试:

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

                  【讨论】:

                    【解决方案9】:

                    多年后,只是另一种方式。不知道它的性能比while循环方法好还是差。

                    对于没有逗号的值,你可以这样做:

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

                    对于通用解决方案,您可以使用分隔符:

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

                    【讨论】:

                      【解决方案10】:
                      $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
                      

                      【讨论】:

                        【解决方案11】:

                        你可以这样做:

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

                        【讨论】:

                          【解决方案12】:

                          如果您不需要该表中的其他信息,您可以只查询您需要的列,这样会更容易:

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

                          【讨论】:

                            猜你喜欢
                            • 2011-03-25
                            • 2021-12-14
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 2015-10-11
                            • 1970-01-01
                            • 2012-11-23
                            • 2018-01-29
                            相关资源
                            最近更新 更多