print_r mysqli_fetch_array

时间:2018-05-02 01:40:43

标签: php mysqli

大家好,我最近在努力处理我的sql查询结果。在做完查询和循环之后。输出显示如下:

Array
(
    [0] => Array
        (
            [0] => 68686.4792
            [Average] => 68686.4792
        )

    [1] => Array
        (
            [0] => 83703.3284
            [Average] => 83703.3284
        )

    [2] => Array
        (
            [0] => 95899.7763
            [Average] => 95899.7763
        )

    [3] => Array
        (
            [0] => 115881.2429
            [Average] => 115881.2429
        )

    [4] => Array
        (
            [0] => 99444.4138
            [Average] => 99444.4138
        )

)

但我期望的是这种格式的输出:

Array
(
    [0] => 68686.4792


    [1] => 83703.3284


    [2] => 95899.7763


    [3] => 115881.2429


    [4] => 99444.4138


)

这是我的PHP代码:

<?php

     $strQuery1 = 'SELECT Avg(Price) as Average From price GROUP by year(Date_of_Transfer)';
     $result1 = $dbhandle->query($strQuery1) or exit("Error code ({$dbhandle->errno}): {$dbhandle->error}");
     $rows=array();
     while($row = mysqli_fetch_array($result1)){
     $rows[] = $row;
  }
     echo'<pre>';
     print_r ($rows);
     echo'</pre>';

?>

我想知道我是否在编码时做错了,或者有什么方法可以将数组转换为我期望的格式。谢谢。

1 个答案:

答案 0 :(得分:0)

这里的选项非常简单,你要么压扁数组,要么将foreach循环一步到数组中。

解决方案1:

// $array = your multidimensional array

$flat_array = array();

foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $k=>$v){

$flat_array[$k] = $v;

}

还记录:http://www.phpro.org/examples/Flatten-Array.html

解决方案2: 已经回答的其他人已经提到了

while($row = mysqli_fetch_array($result1)){ $rows[] = $row['0']; }