模型Codeigniter中的foreach()

时间:2014-04-14 07:36:17

标签: php codeigniter

美好的一天!

我想知道为什么foreach() argument在这段代码中不起作用:

if ($this->db->trans_status() === TRUE) {
    $data = $this->input->post('pTableData');
    $tableData = json_decode($data);

    if (isset($tableData)) {
        foreach ($tableData as $get) {
            $this->insert_activity($id, $get);
        }
    } else {
        echo "NULL";
    }
}

我只想知道var $tableData issethas value是否会转到另一个function inside the model

2 个答案:

答案 0 :(得分:0)

试试这个:

if ($this->db->trans_status() === TRUE)
    {
        $data = $this->input->post('pTableData');
        $tableData = json_decode($data, true); // add a second argument as true to force json_decode to parse it to array
        if (isset($tableData) && is_array($tableData)) // check if the $tableData received is actually an array
            {
                foreach ($tableData as $get) 
                    {
                        $this->insert_activity($id, $get);
                    }
            }
                else
                    {
                        echo "NULL";
                    }
            }

答案 1 :(得分:0)

试试这个:

if (isset($tableData) && is_array( $tableData ) && count( $tableData ) > 0 ){ //is array and has elements
    //your process here with the array
}else{
    // here you proceed to another function like you asked
}