简体   繁体   English

PHP致命错误:无法将字符串偏移量用作数组

[英]PHP Fatal error: Cannot use string offset as an array

Facing a weird situation with arrays.. I am using LinkedIn API to get profile info which returns data in two formats.. 面对数组的怪异情况。我正在使用LinkedIn API来获取配置文件信息,该信息以两种格式返回数据。

If user has just one educational item 如果用户只有一个教育项目

educations=>education=>school-name
educations=>education=>date
...

If more than one education item 如果一项以上的教育项目

educations=>education=>0=>school-name
educations=>education=>0=>date
...
educations=>education=>1=>school-name
educations=>education=>1=>date
...

Now I am trying to make it consistent and convert 现在,我试图使其一致并转换

educations=>education=>school-name

to

educations=>education=>0=>school-name

But getting error in code that i believe should work 但是我认为应该工作的代码中出现错误

if(empty($educations['education'][0]['school-name']))
{
    $temp = array();
    $temp['education'][0]=$educations['education'];
    $educations = $temp;
}

This fails for "just one educational item", generates error on the first line for (isset,is_array and empty) 对于“仅一个教育项目”,此操作失败,在第一行生成错误(isset,is_array和empty)

PHP Fatal error:  Cannot use string offset as an array in ...

print_r returns print_r返回

[educations] => Array
    (
        [education] => Array
            (
                     [id] => 109142639
                     [school-name] => St. Fidelis College
                     [end-date] => Array
                         (
                             [year] => 2009
                         )

            )

    )

Usually you'd write the assignment like this: 通常,您将这样编写作业:

$temp = array(
    "education" => array($educations['education'])
);

To avoid any issues with indexes. 避免索引问题。 This might also fix yours. 这也可能会解决您的问题。

If you're unsure about the contents of $educations['education'][0]['school-name'] you can simply check each part: 如果您不确定$educations['education'][0]['school-name'] ,则可以简单地检查每个部分:

if(isset($educations['education'], $educations['education'][0], $educations['education'][0]['school-name']))

This works because isset doesn't behave like a normal function. 之所以可行,是因为isset行为不像正常函数。 It takes multiple arguments in a lazy manner. 它以懒惰的方式接受多个参数。

You want: 你要:

if(array_key_exists('school-name',$educations['education']))
{
    $educations['education'] = array($educations['education']);
}

Today I experienced the same problem in my application. 今天,我在应用程序中遇到了同样的问题。 Fatal error: Cannot use string offset as an array in /home/servers/bf4c/bf4c.php on line 2447 致命错误:无法在第2447行的/home/servers/bf4c/bf4c.php中将字符串偏移量用作数组

line 2447 2447行

if (!isset($time_played[$player]["started"])) {
                    $time_played[$player]["started"] = $time;
                }

$time_played was overwritten elsewhere and defined as a string. $ time_played在其他地方被覆盖并定义为字符串。 So make sure you do use unique variable names. 因此,请确保使用唯一的变量名。

Here's a tip if you're running through a loop, and it breaks: 如果您正在循环运行,那么这里有个提示,但它会中断:

 if( $myArray != "" ){ // Do your code here echo $myArray['some_id']; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM