0

I have the following array:

Array
(
    [message] => 
    [user_id] => 10
    [errors] => 
    [temp_access_token(emailabc@gmail.com)] => Array
        (
            [access_token] => 99abcdefghijk
            [generated] => 
        )

    [temp_access_token(emaildef@gmail.com)] => Array
        (
            [access_token] => 99klmopqrstuv
            [generated] => 

Here is how the array is being created:

$_SESSION["temp_access_token($username)"] = json_decode($access_token, true);

I am after the following values because I want to store them as $_SESSION variables. The number of temp_access_token/email combinations is always unkown.

[temp_access_token(emailabc@gmail.com)][access_token];
[temp_access_token(emaildef@gmail.com)][access_token];

How do I step through this array, printing the access token for each email address?

sjackson
  • 83
  • 3
  • 9

1 Answers1

3

Do you know the email addresses?

If so you can create a foreach loop, and access each of the arrays.

foreach($emails as $email) {
    $array['temp_access_token('.$email.)']['accesstoken'];
}

If not you can loop through the array, and do a substring of the key, something like this (psuedocode, might not all work in practice)

foreach($array as $key=>$val) {
    if(strpos($key, 'temp_access_token') !== false) {
        //do stuff
    }
}
thomasw_lrd
  • 534
  • 3
  • 13
  • The value for the email address is held in the variable called $username – sjackson May 17 '17 at 19:44
  • $array['temp_access_token('.$username.)']['accesstoken']; You may need to play with the single quotes and doublequotes – thomasw_lrd May 17 '17 at 19:47
  • Still not getting me there. This is what I have now: foreach($_SESSION as $username) { print $_SESSION['temp_access_token('.$username.')']['access_token']."
    "; }
    – sjackson May 17 '17 at 20:03
  • Are you getting any errors? You will need to print_r $username. I don't think that variable contains what you think it does. – thomasw_lrd May 17 '17 at 20:21
  • I'm getting closer...this almost has me there. Just need to tweak it a little. Thank you for your help. foreach ($_SESSION as $temptoken) { print $_SESSION['temp_access_token('.($_SESSION['username']).')']['access_token'] . "
    "; }
    – sjackson May 17 '17 at 20:38
  • Sorry, one more question. If I want to loop through the array, using substring of the key, how would I print the key and matching value? foreach($_SESSION as $key=>$val) { if(strpos($key, 'temp_access_token') !== false) { print $_SESSION['temp_access_token']['access_token']."
    "; } }
    – sjackson May 17 '17 at 22:47
  • You can print the key with $key, and the matching value using strpos function – thomasw_lrd May 18 '17 at 20:42