print_r数组问题

时间:2011-08-19 11:04:21

标签: php arrays loops

我有以下代码:

<?php
    mysql_connect("localhost", "root", "");
    mysql_select_db("coockie");

    $filename ="excelreport.xls";
    $contents = "date \t ip \t visits \t \n";

    $result=mysql_query("select * from test");

    $mydate='';
    $myip='';
    $visits='';

    while($data=mysql_fetch_assoc($result))
    {
        $mydate.=explode(",", $data['date']);
        $myip.=explode(",", $data['ip']);
        $visits.=$data['visits'];

    }

    print_r($mydate);

    //header('Content-type: application/ms-excel');
    //header('Content-Disposition: attachment; filename='.$filename);
    //echo $contents;
?>

$mydate以字符串Array输出。我需要输出像数组的值。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

您滥用字符串连接运算符.=

$mydate.=explode(",", $data['date']);

explode为您提供了一个数组,并使用.=将其转换为字符串Array。正确的方法是使用[]运算符

$mydate=array();
$myip=array();
$visits='';

while($data=mysql_fetch_assoc($result))
{
    $mydate[]=explode(",", $data['date']);
    $myip[]=explode(",", $data['ip']);
    $visits.=$data['visits'];

}

print_r($mydate);

答案 1 :(得分:1)

您可能想要var_dump而不是print_r。至少用于调试目的。

var_dump($mydate);