2

In my foreach, I see the following error:

Fatal error: Cannot use string offset as an array [..] on line 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. Can someone lend a hand?

P.S. I added break; after the last echo and that solved it but I don't think that's the way to go about it.

16
  • what is the output of var_dump($response); or var_dump($result['provider']); Commented Jan 17, 2014 at 8:37
  • 4
    Do a var_dump of $result['info']. Probably doesn't contain what the code suggests it should. Commented Jan 17, 2014 at 8:37
  • @rccoros var_dump($result['provider'] returns string(1) "2". That's weird. Commented Jan 17, 2014 at 8:41
  • it mean's that $response is string not array Commented Jan 17, 2014 at 8:46
  • @sanjeev is_array($response); returns true. Commented Jan 17, 2014 at 8:50

3 Answers 3

1

you have used $result['info'] in a loop. it should be sometimes sub array of $result['info'] is not set. 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:

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Chris Burton : i have updated my answer. can you try this now?
@Chris Burton : can you just remove this line and test if it worked then i will try differently
I just wanted to point out that the problem was the foreach loop. Adding break; after the last echo or simply removing the foreach and using $result = $response['auth']; work equally.
1

$result['info'] is obviously a string and not an array.

Comments

0

You declare $result['info'] as a string somewhere then later try to use it as an array. Try to var_dump $result['info'].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.