print_r数组问题

发布于 2024-11-30 19:06:58 字数 732 浏览 0 评论 0原文

我有以下代码:

<?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。我需要它像值数组一样输出。有什么建议吗?

I have the following code:

<?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 is outputted as string Array. I need it outputted like array of values. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2024-12-07 19:06:58

您误用了字符串连接运算符 .=

$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);

You are misusing string concatenation operator .=

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

explode gives you an array, and with .= it's converted to string Array. The proper way is to use [] operator

$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);
甩你一脸翔 2024-12-07 19:06:58

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

var_dump($mydate);

You might want to try var_dump instead of print_r. At least for debugging purposes.

var_dump($mydate);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文