dongyoucha0645 2016-07-16 07:08
浏览 104
已采纳

Get_post_meta字符串数组到数组

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条回答 默认 最新

  • dousi4900 2016-07-16 07:15
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?