2016-07-16 49 views
1

我想將一個$customFieldName轉換爲一個數組,但我把它作爲字符串取回。我試過如下:Get_post_meta字符串數組到數組

1 TRY

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

2嘗試

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

我回來的字符串如下所示:

「{\ 「use_own_api \」:假,\ 「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 \\」:\\ 「承載\\」,\\ 「expires_in \\」:3600,\\ 「refresh_token \\」:\\ 「1 \/534ewsdcy \\」,\\ 「創建\\」:1467867036} \ 「\ 」ga_active_web_property \「:{\」 __ PHP_Incomplete_Class_Name \ 「:\」 Google_Webproperty \ 「\ 」帳戶ID \「:\ 」7489234 \「,\ 」childLink \「:{\ 」__ PHP_Incomplete_Class_Name \「:\ 」Google_WebpropertyChildLink \「,\ 」HREF \「:\」 HTTPS: \/\/www.googleapis.com \ /分析\/V3 \ /管理\ /賬戶\/7489234 \ /網絡媒體資源\/UA-7489234-1 \ /型材\ 「\ 」類型\「:\」 分析#型材\ 「},\」 創建\ 「:\」 2016-06-28T19:38:17.530Z \ 「\ 」ID \「:\ 」UA-7489234-1 \「,\ 」industryVertical \「:\」 COMPUTERS_AND_ELECTRONICS \「,\」internalWebPropertyId \「:\」11922adsf \「,\」kind \「:\」analytics#webproperty \「,\」level \「:\」「

但是,仍然以字符串形式返回。

任何建議,如何讓陣列回array

我感謝您的回覆!

+0

你有沒有試過這個。 $ jArr = json_decode($ jsonString,true); –

回答

2

我複製你的字符串,但將兩個斜槓\\替換爲三個\\\(我只是將它正確地複製到我的php代碼引用中)然後我在字符串的末尾添加兩個}}在下面的代碼$日結束)有有效的JSON - 之後,我能得到數組:

$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); 

所以可能會試試這個(或類似使用str_replace函數json_decode之前的東西):

$field = json_decode(str_replace("\\\"",'"',get_post_meta((int)$id[0], $customFieldName, true) . '}}'),true); 
+0

Thx您的回覆!我仍然得到一個字符串,而不是一個數組。還有什麼建議? – mrquad

+0

@mrquad我更新了我的答案。 –