0

I have saved arrays in custom post fields and I am retrieving them directly from the db.

$ga_set = get_meta_values_from_all_posts('ganalytics_settings', 'post', 'draft');

    function get_meta_values_from_all_posts( $key = '', $type = 'post', $status = 'publish' ) {

        global $wpdb;

        if( empty( $key ) )
            return;

        $r = $wpdb->get_col( $wpdb->prepare( "
            SELECT pm.meta_value FROM {$wpdb->postmeta} pm
            LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
            WHERE pm.meta_key = '%s' 
            AND p.post_status = '%s' 
            AND p.post_type = '%s'
        ", $key, $status, $type ) );

        return $r;
    }

The result I am getting back looks like the following:

enter image description here

Now I would like to iterate through the array in a for loop:

for ($i = 0; $i < count($ga_set); $i++) {

    $settings= $ga_set[i];
...
}

However, $settings is null.

Any suggestions how to get the value of $ga_set to $settings?

I appreciate your reply!

UPDATE

The string that is stored in $ga_set is

"a:8:{s:11:\"use_own_api\";b:0;s:16:\"google_auth_code\";s:45:\"4/hLXqq9X7sX1sOY-K6MhpEZu6bc8fGGADKLnjlcWA-p4\";s:14:\"google_api_key\";s:39:\"AIzaSyAJjpxVYfZ0WQPZSPr72DOuKU3X-sXquqM\";s:16:\"google_client_id\";s:39:\"180054848980.apps.googleusercontent.com\";s:20:\"google_client_secret\";s:24:\"XtEhZR8lUdRnzx5zdH7KsUaY\";s:19:\"google_access_token\";s:215:\"{\"access_token\":\"ya29.Ci8YA6-Ar2eJC21YOxaZEAuWKWLLgTbq_R_-kjR9Acz4UBF92s0hQGCkSUJfh3BvuQ\",\"token_type\":\"Bearer\",\"expires_in\":3600,\"refresh_token\":\"1/zDGdHo4JTJ6o9TOoZyxnY6e89gUc2UJ6G4\",\"created\":1467867036}\";s:22:\"ga_active_web_property\";O:18:\"Google_Webproperty\":20:{s:9:\"accountId\";s:8:\"79991596\";s:18:\"\u0000*\u0000__childLinkType\";s:27:\"Google_WebpropertyChildLink\";s:22:\"\u0000*\u0000__childLinkDataType\";s:0:\"\";s:9:\"childLink\";O:27:\"Google_WebpropertyChildLink\":2:{s:4:\"href\";s:105:\"https://www.googleapis.com/analytics/v3/management/accounts/79991596/webproperties/UA-79991596-2/profiles\";s:4:\"type\";s:18:\"analytics#profiles\";}s:7:\"created\";s:24:\"2016-06-28T19:40:27.202Z\";s:2:\"id\";s:13:\"U"
Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
  • The $ga_set variable is a serialized array. Unserialized it first before looping though it `$ga_set = unserialize($ga_set)` – z3r0ck Jul 10 '16 at 18:44
  • @z3r0ck the ga setting variable has more than 1 array in it. I tried the following: ` $ganalytics_settings = unserialize($ga_set[i]);` and get `false` back. How would you unserialize all of them? – Carol.Kar Jul 10 '16 at 18:54
  • 1
    If that's the case then use `$settings = unserialize($ga_set)[$i]` inside the loop. – z3r0ck Jul 10 '16 at 18:56
  • @z3r0ck I tried `$settings = unserialize($ga_set)[$i]` and still get `null` back. – Carol.Kar Jul 10 '16 at 19:01
  • please post $ga_set serialized string. – z3r0ck Jul 10 '16 at 19:03
  • @z3r0ck Thx in advance for your help! See my update. – Carol.Kar Jul 10 '16 at 19:12
  • 1
    When i try to unserialize there is an error. [This](http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset) might help you. – z3r0ck Jul 10 '16 at 19:26
  • @z3r0ck Thx for your input. How is my string broken? What should I do to fix this? ...Sorry if this question is stupid, but I do not get it yet. – Carol.Kar Jul 11 '16 at 04:41

0 Answers0