dq1685513999 2017-04-30 15:23
浏览 83
已采纳

它是一个数组,但它表示对foreach的无效参数

I got a problem here, why my array cannot go through foreach ? This is my code, I wonder why it says invalid argument. Please help me, thank you.

source

    /*some of block code here*/

    $content = array();
    $content['table_1'] = array();
    $columns = array('B', 'E', 'H', 'K');
    $rows = array(
        0 => 7,
        1 => 8,
        2 => 9,
        3 => 10,
    );
    foreach ($columns AS $column) {
        foreach ($rows AS $row_key => $row_value) {
            $content['table_1'][$column . $row_value] = $data['jenis_kelamin']['l']['count'][$row_key];
        }
    }

    $columns = array('C', 'F', 'I', 'L');
    foreach ($columns AS $column) {
        foreach ($rows AS $row_key => $row_value) {
            $content['table_1'][$column . $row_value] = $data['jenis_kelamin']['p']['count'][$row_key];
        }
    }
    return $content;

print file

$tableContent = $this->cetak->Statistik_Content()['table_1'];

   if (is_array($tableContent) || $tableContent instanceof Traversable) {
        foreach ($tableContent AS $list) {
            foreach ($list AS $key => $value) { // **This one generates an error**
                $this->excel->getActiveSheet()->setCellValue($key, $value);
            }
        }
    }

If I check it with is array, the condition true, but when I try to put it in foreach, it says invalid, I have no idea, I already look into this problem for hours and still can't fix it.

Var Dump $tableContent

array(32) { ["B7"]=> int(0) ["B8"]=> int(0) ["B9"]=> int(1) ["B10"]=> int(1) ["E7"]=> int(0) ["E8"]=> int(0) ["E9"]=> int(1) ["E10"]=> int(1) ["H7"]=> int(0) ["H8"]=> int(0) ["H9"]=> int(1) ["H10"]=> int(1) ["K7"]=> int(0) ["K8"]=> int(0) ["K9"]=> int(1) ["K10"]=> int(1) ["C7"]=> int(0) ["C8"]=> int(0) ["C9"]=> int(1) ["C10"]=> int(0) ["F7"]=> int(0) ["F8"]=> int(0) ["F9"]=> int(1) ["F10"]=> int(0) ["I7"]=> int(0) ["I8"]=> int(0) ["I9"]=> int(1) ["I10"]=> int(0) ["L7"]=> int(0) ["L8"]=> int(0) ["L9"]=> int(1) ["L10"]=> int(0) }
  • 写回答

2条回答 默认 最新

  • dongwan0574 2017-04-30 15:34
    关注

    There is no need of another foreach if $tableContent is a single dimensional array.

    Try this :

    $tableContent = $this->cetak->Statistik_Content()['table_1'];
    
    if (is_array($tableContent) || $tableContent instanceof Traversable) {
        foreach ($tableContent as $key => $value) {
    
            $this->excel->getActiveSheet()->setCellValue($key, $value);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据