2

I have the following...

/* Request access tokens from twitter */
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);

$_SESSION['access_token'] = $access_token;
echo $access_token[2];

"$access_token" is an array, Im trying to retrieve a value from this array only the above echo doesn't seem to echo anything?

dump var

array(4) { ["oauth_token"]=> string(50) "81920494-vHspkpas4WiOYoFKCgto85mW2XeTxuA130MwcHHWb" ["oauth_token_secret"]=> string(42) "WwIYybFivEwZQ1ORbeqY1irHT385EIuh27alWy9ED4" ["user_id"]=> string(8) "81989494" ["screen_name"]=> string(12) "KlareB" }
2
  • Can you print the results of var_dump($access_token); Commented May 28, 2013 at 19:47
  • You really seem to have a problem comprehending arrays. If the key says ["user_id"], then the key is ["user_id"]. Only if the key says [2], then the key is [2]. Commented May 28, 2013 at 19:50

2 Answers 2

5

If the var_dump is:

array(4) { ["oauth_token"]=> string(50) "81920494-vHspkpas4WiOYoFKCgto85mW2XeTxuA130MwcHHWb" ["oauth_token_secret"]=> string(42) "WwIYybFivEwZQ1ORbeqY1irHT385EIuh27alWy9ED4" ["user_id"]=> string(8) "81920494" ["screen_name"]=> string(12) "KlareBrennan" }

Then echo $access_array[2]; wont work because its not indexed numerically.

You would need to do:

echo $access_array['user_id'];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, didn't realise was so simple
No problem :) its not easy unless you know how to do it.
1

It is necessary to remember than in PHP arrays are actually hashmaps. That is to say that they are an associative array of key => value pairs.

$_SESSION['access_token'] = $access_token;
echo $access_token[2];

This means the above code will look in the $access_token array not at position 2 but at key 2. We can see from your vardump there is no key 2:

array(4) { 
  ["oauth_token"]=> string(50) "81920494-vHspkpas4WiOYoFKCgto85mW2XeTxuA130MwcHHWb
  ["oauth_token_secret"]=> string(42) "WwIYybFivEwZQ1ORbeqY1irHT385EIuh27alWy9ED4" 
  ["user_id"]=> string(8) "81920494" 
  ["screen_name"]=> string(12) "KlareBrennan" 
}

Note that for most indexed arrays this functions exactly the same way, which is what leads to your confusion. Please consider the following code:

<?php

  $blah = array();
  $blah['mykey'] = "My first key.";
  $blah[] = "My second key.";
  $blah[1] = "My third key.";
  $blah[] = "My last key.";

  var_dump($blah);

And it's results:

array(4) {
  ["mykey"]=>
  string(13) "My first key."
  [0]=>
  string(14) "My second key."
  [1]=>
  string(13) "My third key."
  [2]=>
  string(12) "My last key."
}

We can see that if no key is specified the next available key is use; and in this numerals are assigned as keys for the key-value pair, but that they can live alongside string keys. For this reason array_values() is a useful function if you want to iterate over all the contents of an array.

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.