首页 > 编程 > PHP > 正文

PHP警告Cannot use a scalar value as an array

2019-11-08 03:18:08
字体:
来源:转载
供稿:网友

报错代码

foreach ($data as $key => $value) { $strtotime = strtotime($key) + 3600 * 8; $time = intval($strtotime / 86400); try { $cache = Yii::$app->cache->instance('base'); foreach ($channels as $key1 => $value1) { $k = "user:state:USER_V_O#".$value1."#"; $set = $cache->exists($k . $time); if (!$set) { $data[$key][$value1] = 0; } else { $nums = $cache->scard($k . $time); $data[$key][$value1][]=intval($nums); } } } catch (Exception $ex) { //TODO待做 $error = '获取数据库信息失败'; } }

更正代码:

foreach ($data as $key => $value) { $strtotime = strtotime($key) + 3600 * 8; $time = intval($strtotime / 86400); /****处理报错****/ if (!$data[$key]) { $data[$key]= []; } /****结束****/ try { $cache = Yii::$app->cache->instance('base'); foreach ($channels as $key1 => $value1) { $k = "user:state:USER_V_O#".$value1."#"; $set = $cache->exists($k . $time); if (!$set) { $data[$key][$value1] = 0; } else { $nums = $cache->scard($k . $time); $data[$key][$value1][]=intval($nums); } } } catch (Exception $ex) { //TODO待做 $error = '获取数据库信息失败'; } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表