2017-04-20 12 views
1

私のチームメンバーの1人が、オブジェクトをMysqlデータベースに保存しました。私は、オブジェクトのプロパティ値を抽出する必要があります。私はPHPを使用してMySQLからフェッチしようとしているとき、私は文字列としてこれを取得しています。そして、PHPを使用するというアイデアを除いて直接プロパティにアクセスすることはありませんsubstr()機能。print_r配列のオブジェクト文字列をPHPのjsonに変換します

データベースに保存されている下の文字列をオブジェクトに再度変換して、そのプロパティにアクセスできるようにするオプションはありますか?

stdClass Object 
(
[status] => 0 
[environment] => Sandbox 
[receipt] => stdClass Object 
    (
     [quantity] => 1 
     [expires_date] => 2017-04-18 08:56:57 Etc/GMT 
     [is_trial_period] => false 
     [purchase_date] => 2017-04-18 08:51:57 Etc/GMT 
     [product_id] => com.1monthAuto.baseball 
     [original_purchase_date_pst] => 2017-04-18 01:51:58 America/Los_Angeles 
     [original_purchase_date_ms] => 1492505518000 
     [web_order_line_item_id] => 1000000034854560 
     [original_purchase_date] => 2017-04-18 08:51:58 Etc/GMT 
    ) 

[latest_receipt] => MIIT6wYJKoZIhvcNAQcCoIIT3DCCE9gC 
) 
+1

? –

+0

'latest_receipt'にアクセスしたいです。 –

+3

データベースにオブジェクトを格納することは、このような問題につながる恐れがあるため、私にとって恐ろしいデザインパターンのように聞こえます。 – Qirel

答えて

1

これがあなたに役立つと考えてください。

正規表現:/latest_receipt\]\s*=>\s*\K[^\s\)]+/

latest_receipt\]\s*=>\s*これは

\K、これはすべての空間(\s)と)

除いて一致する前回のマッチ

[^\s\)]+をリセットするlatest_receipt]スペース=>スペース

一致します

Try this code snippet here

<?php 

ini_set('display_errors', 1); 
$string='stdClass Object 
(
[status] => 0 
[environment] => Sandbox 
[receipt] => stdClass Object 
    (
     [quantity] => 1 
     [expires_date] => 2017-04-18 08:56:57 Etc/GMT 
     [is_trial_period] => false 
     [purchase_date] => 2017-04-18 08:51:57 Etc/GMT 
     [product_id] => com.1monthAuto.baseball 
     [original_purchase_date_pst] => 2017-04-18 01:51:58 America/Los_Angeles 
     [original_purchase_date_ms] => 1492505518000 
     [web_order_line_item_id] => 1000000034854560 
     [original_purchase_date] => 2017-04-18 08:51:58 Etc/GMT 
    ) 

[latest_receipt] => MIIT6wYJKoZIhvcNAQcCoIIT3DCCE9gC 
)'; 
preg_match("/latest_receipt\]\s*=>\s*\K[^\s\)]+/", $string,$matches); 
print_r($matches); 

解決方法2:or you can Try this library //このソリューションはjsonprint_rを変換し、かつ確実に動作します。文字列としてlatest_receiptコンテンツを持つように正規表現とstr_replaceを使用して

Try this code snippet hereCopy paste below code and check

<?php 
ini_set('display_errors', 1); 
/** 
* @author Sahil Gulati <[email protected]> 
*/ 
echo printr_source_to_json(
     print_r(
       array("Name"=>"Sahil Gulati", 
         "Education"=>array(
          "From"=>array(
           "DU"=>array(
            "Course"=>"B.Sc. (Hons.) Computer Science.") 
          ) 
         ) 
        ) 
       , true 
       ) 
     ); 
/** 
* This function will convert output string of `print_r($array)` to `json string` 
* @note Exceptions are always there i tried myself best to get it done. Here $array can be array of arrays or arrays of objects or both 
* @param String $string This will contain the output of `print_r($array)` (which user will get from ctrl+u of browser), 
* @return String 
*/ 
function printr_source_to_json($string) 
{ 
    /** 
    *replacing `stdClass Objects (` to `{` 
    */ 
    $string = preg_replace("/stdClass Object\s*\(/s", '{ ', $string); 

    /** 
    *replacing `Array (` to `{` 
    */ 
    $string = preg_replace("/Array\s*\(/s", '{ ', $string); 
    /** 
    *replacing `)\n` to `},\n` 
    * @note This might append , at the last of string as well 
    * which we will trim later on. 
    */ 
    $string = preg_replace("/\)\n/", "},\n", $string); 

    /** 
    *replacing `)` to `}` at the last of string 
    */ 
    $string = preg_replace("/\)$/", '}', $string); 
    /** 
    *replacing `[ somevalue ]` to "somevalue" 
    */ 
    $string = preg_replace("/\[\s*([^\s\]]+)\s*\](?=\s*\=>)/", '"\1" ', $string); 
    /** 
    * replacing `=> {` to `: {` 
    */ 
    $string = preg_replace("/=>\s*{/", ': {', $string); 
    /** 
    * replacing empty last values of array special case `=> \n }` to : "" \n 
    */ 
    $string = preg_replace("/=>\s*[\n\s]*\}/s", ":\"\"\n}", $string); 

    /** 
    * replacing `=> somevalue` to `: "somevalue",` 
    */ 
    $string = preg_replace("/=>\s*([^\n\"]*)/", ':"\1",', $string); 
    /** 
    * replacing last mistakes `, }` to `}` 
    */ 
    $string = preg_replace("/,\s*}/s", '}', $string); 
    /** 
    * replacing `} ,` at the end to `}` 
    */ 
    return $string = preg_replace("/}\s*,$/s", '}', $string); 
} 
+0

+1これは値を抽出する方が良い考えです。この文字列をオブジェクトにするために使用できるオプションがありますので、他のプロパティ値も抽出できますか? –

+0

@ArunJainこれも同様にチェックさせてください。 –

+0

@ArunJain私はあなたが助けてくれることを願ってポストするつもりの新しいソリューションを作成しました –

0

$dictionary="stdClass Object 
    (
    [status] => 0 
    [environment] => Sandbox 
    [receipt] => stdClass Object 
     (
      [quantity] => 1 
      [expires_date] => 2017-04-18 08:56:57 Etc/GMT 
      [is_trial_period] => false 
      [purchase_date] => 2017-04-18 08:51:57 Etc/GMT 
      [product_id] => com.1monthAuto.baseball 
      [original_purchase_date_pst] => 2017-04-18 01:51:58 America/Los_Angeles 
      [original_purchase_date_ms] => 1492505518000 
      [web_order_line_item_id] => 1000000034854560 
      original_purchase_date] => 2017-04-18 08:51:58 Etc/GMT 
     ) 

    [latest_receipt] => MIIT6wYJKoZIhvcNAQcCoIIT3DCCE9gC 
    )"; 
preg_match('/.*\[latest_receipt\].*$/m', $dictionary, $matches); 
$receipt = str_replace('[latest_receipt] => ', '', $matches[0]); 
echo $receipt; 

あなたはそれの機能にし、抽出するために、変数にlatest_receipt置き換えることができますあなたの他のデータ

しかし、あなたが本当にJSONオブジェクトとして保存について考える必要があるか、このデータを格納するための新しい列を作成するには..あなたがアクセスしたいプロパティ

関連する問題