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

时间:2017-04-30 15:23:30

标签: php arrays

我在这里遇到了一个问题,为什么我的阵列无法通过foreach?这是我的代码,我想知道为什么它说无效的论点。请帮帮我,谢谢。

来源

    /*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;

打印文件

$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);
            }
        }
    }

如果我检查它是数组,条件为true,但是当我尝试将它放在foreach中时,它表示无效,我不知道,我已经查看了这个问题几个小时仍然无法修复它。

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 个答案:

答案 0 :(得分:0)

如果foreach是单维数组,则无需另外$tableContent

试试这个:

$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 :(得分:0)

你应该删除一个foreach:

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

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