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 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址