1

I am trying to convert a $customFieldName to an array, but I get it back as string. I tried the following:

1 Try

    $field = array();
    $field = get_post_meta((int)$id[0], $customFieldName, true);

2 Try

    $field = array();
    $field = unserialize(get_post_meta((int)$id[0], $customFieldName, true));

The string I get back looks like the following:

"{\"use_own_api\":false,\"google_auth_code\":\"4\/hLXqq9X7sX1sOY-K6MhpEZu6bc8fGGADKLnjlcWA-p4\",\"google_api_key\":\"AIzaSyAJjpxVYfZ0WQPZSPr72DOuKU3X-sXquqM\",\"google_client_id\":\"180054848980.apps.googleusercontent.com\",\"google_client_secret\":\"sdfsd\",\"google_access_token\":\"{\\"access_token\\":\\"ya29.Ci8YA6-sfsd-asdfas\\",\\"token_type\\":\\"Bearer\\",\\"expires_in\\":3600,\\"refresh_token\\":\\"1\/534ewsdcy\\",\\"created\\":1467867036}\",\"ga_active_web_property\":{\"__PHP_Incomplete_Class_Name\":\"Google_Webproperty\",\"accountId\":\"7489234\",\"childLink\":{\"__PHP_Incomplete_Class_Name\":\"Google_WebpropertyChildLink\",\"href\":\"https:\/\/www.googleapis.com\/analytics\/v3\/management\/accounts\/7489234\/webproperties\/UA-7489234-1\/profiles\",\"type\":\"analytics#profiles\"},\"created\":\"2016-06-28T19:38:17.530Z\",\"id\":\"UA-7489234-1\",\"industryVertical\":\"COMPUTERS_AND_ELECTRONICS\",\"internalWebPropertyId\":\"11922adsf\",\"kind\":\"analytics#webproperty\",\"level\":\""

However, still I get it back as string.

Any suggestions, how to get the array back as array.

I appreciate your reply!

1
  • Have you tried this. $jArr = json_decode($jsonString, true); Commented Jul 16, 2016 at 7:12

1 Answer 1

1

I copy your string but replace two slashes \\ to three \\\ (I only do it to properly copy your string into my php code quotes) then I and add two }} at the end of the string (look at end of $st in code below) to have valid json - after that I was able to get array:

$st = '{\"use_own_api\":false,\"google_auth_code\":\"4\/hLXqq9X7sX1sOY-K6MhpEZu6bc8fGGADKLnjlcWA-p4\",\"google_api_key\":\"AIzaSyAJjpxVYfZ0WQPZSPr72DOuKU3X-sXquqM\",\"google_client_id\":\"180054848980.apps.googleusercontent.com\",\"google_client_secret\":\"sdfsd\",\"google_access_token\":\"{\\\"access_token\\\":\\\"ya29.Ci8YA6-sfsd-asdfas\\\",\\\"token_type\\\":\\\"Bearer\\\",\\\"expires_in\\\":3600,\\\"refresh_token\\\":\\\"1\/534ewsdcy\\\",\\\"created\\\":1467867036}\",\"ga_active_web_property\":{\"__PHP_Incomplete_Class_Name\":\"Google_Webproperty\",\"accountId\":\"7489234\",\"childLink\":{\"__PHP_Incomplete_Class_Name\":\"Google_WebpropertyChildLink\",\"href\":\"https:\/\/www.googleapis.com\/analytics\/v3\/management\/accounts\/7489234\/webproperties\/UA-7489234-1\/profiles\",\"type\":\"analytics#profiles\"},\"created\":\"2016-06-28T19:38:17.530Z\",\"id\":\"UA-7489234-1\",\"industryVertical\":\"COMPUTERS_AND_ELECTRONICS\",\"internalWebPropertyId\":\"11922adsf\",\"kind\":\"analytics#webproperty\",\"level\":\""' . '}}';
$strWithoutSlash = str_replace("\\\"",'"',$st);
$array = json_decode($strWithoutSlash,true);

So may be try this (or something similar using str_replace before json_decode):

 $field = json_decode(str_replace("\\\"",'"',get_post_meta((int)$id[0], $customFieldName, true) . '}}'),true);
Sign up to request clarification or add additional context in comments.

1 Comment

Thx for your reply! I still get a string back instead of an array. Any further suggestions?

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.