简体   繁体   English

致命错误:不能将字符串偏移量用作数组?

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

In my foreach, I see the following error: 在我的foreach中,我看到以下错误:

Fatal error: Cannot use string offset as an array [..] on line 97 致命错误:无法 在第97行上将 字符串偏移量用作数组 [..]

 88.    foreach ($response as $result) {
 89.        if($result['provider'] == 'Facebook') {
 90.            $provider = 'facebook';
 91.        }else{
 92.            $provider = 'twitter';
 93.        }
 94.        $user = array(
 95.            'provider' => $result['provider'],
 96.            'id' => $result['uid'],
 97.            'name' => $result['info']['name'],
 98.            'image' => $result['info']['image'],
 99.            'link' => $result['info']['urls'][$provider],
100.        );
101.        echo "<h1>".$user['provider']."</h1>";
102.        echo "<p>".$user['id']."</p>";
103.        echo '<p><img src="'.$user['image'].'" /></p>';
105.        echo "<p>".$user['name']."</p>";
106.        echo "<p>".$user['link']."</p>";
107.    }

I've tried reading around the web to understand what the problem is but it seems that some issues are unrelated to mine. 我尝试在网络上阅读以了解问题所在,但似乎有些问题与我的问题无关。 I should also just come out with it and let you know I'm not that great with PHP. 我也应该随便问一下它,并让您知道我对PHP不太满意。 Can someone lend a hand? 有人可以伸出援手吗?

PS I added break; PS我加了break; after the last echo and that solved it but I don't think that's the way to go about it. 在最后一个回声之后,它解决了,但我认为这不是解决问题的方法。

$result['info']显然是一个字符串,而不是一个数组。

Try This.In line 99 you should not use , at the end... 尝试一下。在第99行中,最后不应该使用,

 88.    foreach ($response as $result) {
 89.        if($result['provider'] == 'Facebook') {
 90.            $provider = 'facebook';
 91.        }else{
 92.            $provider = 'twitter';
 93.        }
 94.        $user = array(
 95.            'provider' => $result['provider'],
 96.            'id' => $result['uid'],
 97.            'name' => $result['info']['name'],
 98.            'image' => $result['info']['image'],
 99.            'link' => $result['info']['urls'][$provider]
 100.        );
 101.        echo "<h1>".$user['provider']."</h1>";
 102.        echo "<p>".$user['id']."</p>";
 103.        echo '<p><img src="'.$user['image'].'" /></p>';
 105.        echo "<p>".$user['name']."</p>";
 106.        echo "<p>".$user['link']."</p>";
 107.    }

you have used $result['info'] in a loop. 您已在循环中使用$result['info'] it should be sometimes sub array of $result['info'] is not set. 有时应该未设置$result['info']子数组。 It seems that you do not need to run loop. 看来您不需要运行循环。 because you will have single user info at a time. 因为您一次将只有一个用户信息。 or you can use break like this: 或者您可以像这样使用break:

foreach ($response as $result) {
    $provider = strtolower($result['provider']);
    $user = array(
        'provider' => $result['provider'],
        'id' => $result['uid'],
        'name' => isset($result['info']['name']) ? $result['info']['name'] : '',
        'image' => isset($result['info']['image']) ? $result['info']['image'] : '',
        'link' => isset($result['info']['urls'][$provider]) ? $result['info']['urls'][$provider] : ''
    );
print_r($user);
    echo "<h1>" . $user['provider'] . "</h1>";
    echo "<p>" . $user['id'] . "</p>";
    echo '<p><img src="' . $user['image'] . '" /></p>';
    echo "<p>" . $user['name'] . "</p>";
    echo "<p>" . $user['link'] . "</p>";
    break;
}

You declare $result['info'] as a string somewhere then later try to use it as an array. 您将$ result ['info']声明为字符串,然后再尝试将其用作数组。 Try to var_dump $result['info']. 尝试var_dump $ result ['info']。

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

相关问题 “不能将字符串偏移量用作数组”致命错误 - “Cannot use string offset as an array” Fatal Error 致命错误:不能将字符串偏移用作数组 - Fatal error: Cannot use string offset as an array 致命错误:无法将字符串偏移量用作数组 - Fatal error: Cannot use string offset as an array PHP 7 致命错误:未捕获错误:无法使用字符串偏移量作为数组 - PHP 7 Fatal error: Uncaught Error: Cannot use string offset as an array 致命错误:未捕获错误:无法使用字符串偏移量作为数组 - Fatal error: Uncaught Error: Cannot use string offset as an array 使用变量时:致命错误:不能将字符串偏移量用作数组 - when using variable: Fatal error: Cannot use string offset as an array PHP致命错误:无法将字符串偏移量用作数组 - PHP Fatal error: Cannot use string offset as an array 致命错误:不能将字符串偏移用作数组 - 从JSON响应中解析字符串 - Fatal error: Cannot use string offset as an array - parsing string from JSON response 致命错误:无法将字符串偏移量用作数组-比较数组值的正确方法是什么 - Fatal error: Cannot use string offset as an array - What the right way to compare array value 越来越令人讨厌的错误:“致命错误:无法在…中使用字符串偏移量作为数组” - getting really annoying error: “Fatal error: Cannot use string offset as an array in…”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM