我有一个我需要的PHP多维阵列 有选择地 打印到Excel作家。我有这样的东西:

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

我认为我可以管理“写作”以表现出色,但是我似乎无法获得PHP来选择性地编写每个字段的值以及我想要的方式。我已经阅读了很多东西,并且尝试过很多事情,我认为答案是使用两个foreach循环作为“初始”阵列,另一个用于第二维数组,但是由于某种原因,我无法使其正常工作。我也尝试使用“提取物”,但进展不佳……我不是“训练”的PHP程序员。 Google一直是我的大学(并且是我的教职员工),尽管我没有太多麻烦来处理阵列...,但世界上的多维阵列颠倒了...

任何帮助将不胜感激。

谢谢!

-----编辑-----好吧,我不需要“导出到Excel”功能,一旦可以将数组的值分配给变量,我就可以处理该功能。

我目前正在这样做:

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

但是我得到的是

1023610236102361023610236102361023610236102361023610236102361023610236102361023610236102361023610236102361023610236

这是我的第一个代理商(我将查询限制为1-限制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, $TALK, ,等...)在我已经创建的电子表格中 PEAR::EXCEL_WRITER. 。当然,您需要创建一个循环以迭代不同 $USERS 您的查询将输出。

有帮助吗?

解决方案

该脚本可以生成File 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>

其他提示

这只是在此处发布的答案的补充...它只是显示了如何在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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top