美好的一天!
我想知道为什么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 isset
或has value
是否会转到另一个function inside the model
。
答案 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
}