在制作tp5的时候 显示 未定义数组索引 如何解决呢?
{volist name="navData" id="vo"}<div><h3>{$vo.cat_name}</h3><div>{volist name="vo.child" id="vi"}<dl><dt>{$vi.cat_name}</dt><dd>{volist name="vi.child" id="vp"}{$vp.cat_name}{/volist}</dd></dl>{/volist}</div></div>{/volist}
原因是某些数组没有child字段
解决办法 :加个判断 如果没有child字段 就赋予其空值
解决办法 :加个判断 如果没有child字段 就赋予其空值
public function getNavData(){$return = array();$allData = db('category')->select();foreach ($allData as $k => $v){if ($v['pid'] == 0){if (!isset($v['child'])){$v['child'] = array();}foreach ($allData as $k1 => $v1){if ($v1['pid'] == $v['id']){if (!isset($v1['child'])){$v1['child'] = array();}foreach ($allData as $k2 => $v2){if ($v2['pid'] == $v1['id']){$v1['child'][] = $v2;}}$v['child'][] = $v1;}}$return[] = $v;}}return $return;}
PS : tp5中通过模型select返回的是对象 通过db select返回的是数组