2016-07-16 30 views
1

$customFieldNameを配列に変換しようとしていますが、文字列として戻します。私は、次のことを試してみました:Get_post_meta配列への文字列配列

1、私は戻って取得文字列は次のようになります

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

を試してみてください

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

2を試してみてください:\ {」

"use_own_api \":偽、\ "google_auth_code \":\ "4 \/hLXqq9X7sX1sOY \ "google_client_secret \":\ "svfsd \"、\ "google_client_id \":\ "180054848980.apps.googleusercontent.com \"、\ " \\ "token_type \\":\\ "bearer \\"、\\ "bearer \\"、\\ "google_access_token \":\\ "access_token \\":\\ "ya29.Ci8YA6-sfsd-asdfas \\" "expires_in \\":3600、\\ "refresh_token \\":\\ "1 \/534ewsdcy \\"、\\ "created \\":1467867036} \ "、\" ga_active_web_property \ ":\\ PHP_Incomplete_Class_Name : "" Google_WebpropertyChildLink \ "、\" href \ ":\" https:\ ":\" Google_Webproperty \ "、\" accountId \ ":\" 7489234 \ "、\" childLink \ ":\" PHP_Incomplete_Class_Name \ \/\/analytics \/v3 \/management \/accounts \/7489234 \/webproperties \/UA-7489234-1 \/profiles \ "、\" type \ ":\" analytics# \ "created \":\ "2016-06-28T19:38:17.530Z \"、\ "id \":\ "UA-7489234-1 \"、\ "industryVertical \":\ " "" \ "種類\":\ "アナリティクス#ウェブプロパティ\"、\ "レベル\":\ ""

しかし、まだ私はそれを文字列として返します。

アレイをarrayに戻す方法を教えてください。

返信いただきありがとうございます。

+0

これを試してみましたか? $ jArr = json_decode($ jsonString、true); –

答えて

2

私は文字列をコピーしますが、2つのスラッシュ\\を3つの\\に置き換えます(文字列をPHPコード引用符に正しくコピーするためにのみ行います)。次に文字列の最後に2つ}}}有効なJSONを持ってするには、以下のコードで$ ST)の終わりに - その後、私は配列を取得することができました:

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

ので)json_decode前str_replaceを使用して、これを試してください(または似たようなことがあります

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

あなたの返信のためのThx!私はまだ配列の代わりに文字列を取得します。それ以上の提案はありますか? – mrquad

+0

@mrquad答えを更新します。 –