Home > Mobile >  Modify get_option array values and update_option
Modify get_option array values and update_option

Time:03-02

So I want to get the saved database option and then update it with the API return and then save the option in the database again, but I'm having some issues.

So I have the following method:

public static function refresh_access_token(string $user_id, string $access_token): bool
{
    if (!$authenticated_users = get_option('instagram_authenticated_users')) {
        return false;
    }

    // The code for the cURL request is here (It's not needed for this question).

    $array = json_decode($curl_exec);

    foreach ($authenticated_users as $user) {
        if ($user['user_id'] === (int) $user_id) {
            $user['access_token'] = $array->access_token;
            $user['access_token_expiration'] = time()   $array->expires_in;
            $user['last_updated'] = (string) time();
        }
    }

    return update_option('instagram_authenticated_users', $user);
}

The cURL response $array gives me the following:

$array = {stdClass} [3]
   access_token = "IGQVJVV1YzUEdMb1lDajNZAcmRxakR3V0dkOXUyWHdtSGM0ZAHFYempkX0VHYVlKYTVYMkNxYVVpNVFuclZAlWVRsbmktNjN2cG1ISEJ4T2VJWUd0M2JBMGcyUlFXOWFlTjdEVDhKaEJB"
   token_type = "bearer"
   expires_in = {int} 5099901

Now within $authenticated_users, I have an array of arrays which outputs:

$authenticated_users = {array} [2]
 [0] = {array} [5]
    username = "semi_test1"
    user_id = {int} 17841449642220098
    access_token = "IGQVJWMDJnblBnUGMtMTVFa1NpUGdValNBVUZAyZAWM2OTdSSkRFdmNUbnVOQXJqeFhwbDVmT0c3aXJfamFYdnZANSlpXc3Mwc05PS0tMSzNsbXhES0tkTzNoOEY3RFRIb3dsblBiTXN3"
    access_token_expiration = {int} 1651281005
    last_updated = {int} 1646181108
 [1] = {array} [5]
    username = "semi_test2"
    user_id = {int} 17841400835712753
    access_token = "IGQVJVN3VOaUJrU2NzdGxWVTlwaXFLT2h1bnpFU3FKaEpOUGNPeWh2SkpjdHpnRXkyaGJ3NDZArXzJvRWFHdVRqZAEFfN0RodjV4cHQ2YTliSmhyVThUSjlCc1paLV9Fd2dqbzI1b25B"
    access_token_expiration = {int} 1651281136
    last_updated = {int} 1646181136

Now I'm looking at my $user_id param and using the foreach to compare them to the user_id inside an array, and if it matches, update the values inside the array and then update the options, but also retain the other array that hasn't been changed.

I've tried everything and it doesn't seem like it's working and the update_option('instagram_authenticated_users') doesn't update the values and it retains the same data.

So I want get_option('instagram_authenticated_userss') when called after the update to be the following with the new data (The second index should have been updated):

$authenticated_users = {array} [2]
 [0] = {array} [5]
    username = "semi_test1"
    user_id = {int} 17841449642220098
    access_token = "IGQVJWMDJnblBnUGMtMTVFa1NpUGdValNBVUZAyZAWM2OTdSSkRFdmNUbnVOQXJqeFhwbDVmT0c3aXJfamFYdnZANSlpXc3Mwc05PS0tMSzNsbXhES0tkTzNoOEY3RFRIb3dsblBiTXN3"
    access_token_expiration = {int} 1651281005
    last_updated = {int} 1646181108
 [1] = {array} [5]
    username = "semi_test2"
    user_id = {int} 17841400835712753
    access_token = "IGQVJYOEdqQ0hpbmZAHWlFsdDdMNHdUN1hmenhlV2ZAYOTBtMTJiaFhtSjhyUW9DVm9UREtLZAlFQVHhuVE1XUUFBNUF5SHoxdWRJNXd5dUF6ZAkNKeEtNYmVzRzNTWXdGSmhldG9ILTdn" (NEW VALUE)
    access_token_expiration = {int} 1651282134 (NEW VALUE)
    last_updated = {int} 1646181136 (NEW VALUE)

Can someone spot that I might be doing wrong?

CodePudding user response:

The problem is your foreach is not creating references. You have to explicitly tell it to on which variables.

This solution doesn't try to add in the code you need where you want it to retain both the previous and the current access tokens. But this solution does you show how to change an array "in-place" using foreach, which will correct (or help) in correcting the problem of having the changes stored to the db.

Replace your foreach statement with:

foreach ($authenticated_users as &$user) {

That ampersand on &$user will make the difference: it will create $user by reference. Any changes to it will change $authenticated_users too.

Example:

$a = array("1","apple","3","pear");

foreach ($a as $p) {
    if ($p == "apple") $p = "sauce";
}

print_r($a);

/* Outputs:
Array
(
    [0] => 1
    [1] => apple
    [2] => 3
    [3] => pear
)
*/

foreach ($a as &$p) {
    if ($p == "apple") $p = "sauce";
}

print_r($a);

/* Outputs:
Array
(
    [0] => 1
    [1] => sauce
    [2] => 3
    [3] => pear
)
*/

The second foreach changes the source array because of the ampersand sign on $p in the foreach statement.

In this way, the same approach could work for you.

  •  Tags:  
  • php
  • Related