有选择地打印多维数组

时间:2013-11-07 01:44:06

标签: php arrays excel multidimensional-array writer

我有一个PHP多维数组,我需要有选择地打印到excel writer。我有这样的事情:

array(2) {
  [10227]=>
  array(9) {
    ["user"]=>
    string(5) "10227"
    ["talk_time"]=>
    string(1) "8"
    ["acw"]=>
    string(1) "0"
    ["idle"]=>
    string(1) "6"
  }
  [10236]=>
  array(9) {
    ["user"]=>
    string(5) "10236"
    ["talk_time"]=>
    string(2) "10"
    ["acw"]=>
    string(1) "3"
    ["idle"]=>
    string(1) "0"
  }
}

在excel中,我需要它看起来像这样:

User  | talk_time  | acw  | idle
10227 | 8          | 0    | 6
10236 | 10         | 3    | 0

我认为我可以管理excel的“写入”,但我似乎无法让php有选择地为每个字段写入值以及我想要的方式。我已经阅读了很多并尝试了很多东西,我认为答案是使用两个foreach循环,一个用于'initial'数组,另一个用于第二个维数组,但由于某种原因,我无法使其工作。我也试过使用'extract',但它不太顺利......我不是一个'训练有素'的PHP程序员。谷歌一直是我的大学(和我的教员一样,虽然我在使用数组时没有太多麻烦......),世界颠倒了多维数组......

任何帮助都会非常感激。

谢谢!

- - - - - - - - EDIT 好吧,我不需要'导出到Excel'功能,一旦我可以将数组的值赋给变量,我就可以处理。

我目前正在这样做:

foreach ($agents as $row){
  $USER=$row["user"];
  echo "$USER\n";
  foreach($row as $col){
  $TALK_SEC=$col["talk_sec"];
  }
}

但我得到的只是

1023610236102361023610236102361023610236102361023610236102361023610236102361023610236102361023610236102361023610236

这是我的第一个代理(我将我的查询限制为1 -LIMIT 1-)次24次。如果我向$TALK_SEC添加一个回音,我会得到我正在查询的四个字段中的每一个的第一个数字,即代理将“吐出”的24倍......

最终编辑和答案

我能够使用实际的单个foreach语句来实现它:

foreach ($agents as $row){
    //VAR_DUMP($row);
    $USER=$row['user'];
    $TALK=$row['talk_time'];
    $ACW=$row['acw'];
    $IDLE=$row['idle'];

现在我所要做的就是在我用$USER创建的电子表格中打印变量名称($TALKPEAR::EXCEL_WRITER等...)。当然,您需要创建一个循环来迭代查询将输出的不同$USERS

2 个答案:

答案 0 :(得分:0)

此脚本可以生成文件excel。但在您的情况下,您需要格式化您的数组:

User  | talk_time  | acw  | idle
10227 | 8          | 0    | 6
10236 | 10         | 3    | 0

剧本:

<?php

$file="demo.xls";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file");
$array = array(
    array(10, 15, 20),
    array(10, 15, 20),
    array(10, 15, 20)
);
?>
<table>
<?php foreach($array as $row): ?>
    <tr>
    <?php foreach($row as $col):?>
        <td><?php echo $col ?></td>
    <?php endforeach; ?>
    </tr>
<?php endforeach; ?>
</table>

答案 1 :(得分:0)

这只是对此处发布的答案的补充......它只是展示了如何在Excel文件中包含数组的标题。将对方的帖子标记为已回答,而不是我的帖子,如果这对您有效。

输出:

user    talk_time   acw      idle
10227   8           0        6
10236   10          3        0

代码:

<?php
$array = 
      array (
        10227 => 
        array (
          'user' => '10227',
          'talk_time' => '8',
          'acw' => '0',
          'idle' => '6',
        ),
        10236 => 
        array (
          'user' => '10236',
          'talk_time' => '10',
          'acw' => '3',
          'idle' => '0',
        ),
      );  
$file="demo.xls";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file");

$index = array_slice($array,0,1);  
$index = array_keys($index[0]);   

?>
<table> 
    <tr>
      <?php foreach($index as $heading): ?>
        <th><?php echo $heading ?></th>
      <?php endforeach; ?>
    </tr> 
    <?php foreach($array as $row): ?>
    <tr>
       <?php foreach($row as $col):?>
          <td><?php echo $col ?></td>
       <?php endforeach; ?>
   </tr>
   <?php endforeach; ?>
</table>