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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化