您的位置:首页 > 产品设计 > UI/UE

PHP警告Cannot use a scalar value as an array

2017-02-17 13:52 621 查看
报错代码

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 = '获取数据库信息失败';
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 数组