[英]PHP 7 Fatal error: Uncaught Error: Cannot use string offset as an array
[英]PHP Fatal error: Cannot use string offset as an array
面对数组的怪异情况。我正在使用LinkedIn API来获取配置文件信息,该信息以两种格式返回数据。
如果用户只有一个教育项目
educations=>education=>school-name
educations=>education=>date
...
如果一项以上的教育项目
educations=>education=>0=>school-name
educations=>education=>0=>date
...
educations=>education=>1=>school-name
educations=>education=>1=>date
...
现在,我试图使其一致并转换
educations=>education=>school-name
至
educations=>education=>0=>school-name
但是我认为应该工作的代码中出现错误
if(empty($educations['education'][0]['school-name']))
{
$temp = array();
$temp['education'][0]=$educations['education'];
$educations = $temp;
}
对于“仅一个教育项目”,此操作失败,在第一行生成错误(isset,is_array和empty)
PHP Fatal error: Cannot use string offset as an array in ...
print_r返回
[educations] => Array
(
[education] => Array
(
[id] => 109142639
[school-name] => St. Fidelis College
[end-date] => Array
(
[year] => 2009
)
)
)
通常,您将这样编写作业:
$temp = array(
"education" => array($educations['education'])
);
避免索引问题。 这也可能会解决您的问题。
如果您不确定$educations['education'][0]['school-name']
,则可以简单地检查每个部分:
if(isset($educations['education'], $educations['education'][0], $educations['education'][0]['school-name']))
之所以可行,是因为isset
行为不像正常函数。 它以懒惰的方式接受多个参数。
你要:
if(array_key_exists('school-name',$educations['education']))
{
$educations['education'] = array($educations['education']);
}
今天,我在应用程序中遇到了同样的问题。 致命错误:无法在第2447行的/home/servers/bf4c/bf4c.php中将字符串偏移量用作数组
2447行
if (!isset($time_played[$player]["started"])) {
$time_played[$player]["started"] = $time;
}
$ time_played在其他地方被覆盖并定义为字符串。 因此,请确保使用唯一的变量名。
如果您正在循环运行,那么这里有个提示,但它会中断:
if( $myArray != "" ){ // Do your code here echo $myArray['some_id']; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.