我有一个示例代码:
$text = 'a:3:{s:2:"id";s:18:"nokiainternal_ver1";s:10:"user_agent";s:13:"NokiaInternal";s:9:"fall_back";s:13:"nokia_generic";}';
$text = unserialize($text);
if(isset($text['model_name'])) {
$text['model_name'] = 'Nokia';
}
echo $text['model_name'];
echo $text['user_agent'];
但结果错误Notice: Undefined index: model_name ...
如何解决?
答案 0 :(得分:2)
以下代码:
if(isset($text['model_name'])) {
$text['model_name'] = 'Nokia';
}
检查是否设置了model_name
,如果设置了,则将其设置为“诺基亚”
这意味着您没有将密钥model_name
设置为任何导致Undefined index
错误的密钥。
尝试:
if(!isset($text['model_name'])) {
$text['model_name'] = 'Nokia';
}