数组值的简单回显

时间:2015-08-21 16:01:58

标签: php arrays

php新手,试图了解数组。

以下代码有什么问题?我需要回显结果,以便将它们传递到另一个页面。

<?php 

$stmt = $conn -> prepare("
    SELECT 
        MONTHNAME(TimeStamp), COUNT(*)
    FROM
        transactions
    WHERE
        TimeStamp BETWEEN '2014-01-01' AND '2014-12-31'
    GROUP BY EXTRACT(MONTH FROM TIMESTAMP);");

$stmt -> execute();

$stmt -> bind_result($month, $days);

while ($stmt -> fetch()) {
        $data[] = array(
        'month' => $month,
        'day' => $day);
    }

print_r($data);

$data = array();

foreach($data as $row) {
    echo $row['month'];
    echo $row['day'];
}

$stmt->close();

?> 

当我print_r($data)时,我可以按以下格式看到阵列正常。

Array ( [0] => Array ( [month] => January [day] => 323537 ) [1] => Array ( [month] => February [day] => 217304 ) [2] => Array (...

1 个答案:

答案 0 :(得分:1)

print_r($data);
**$data = array();**

foreach($data as $row) {
    echo $row['month'];
    echo $row['day'];
}

你print_r你的$ data,然后你用$ data = array()将它设置为一个空数组,而在foreach中你试图通过一个空数组。

解决方案:

print_r($data);

foreach($data as $row) {
    echo $row['month'];
    echo $row['day'];
}

$data = array();