在检查项数组()的设置时,在php中使用unserialize()时出错?

时间:2012-07-26 03:50:42

标签: php

我有一个示例代码:

$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 ...如何解决?

1 个答案:

答案 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'; 
}