2016-09-22 6 views
2

API呼び出しの使用配列内に格納された一連のデータオブジェクトを取得していますが、返されるオブジェクトの一部のみを出力したいだけです。配列内の明示的オブジェクトへのアクセス

すべてのデータは、$mail変数に格納されます。

""" 
[\n 
    {\n 
    "count_purchased": 0,\n 
    "delivered": 1,\n 
    "clicked_unique": 0,\n 
    "shared": 0,\n 
    "mailings": 1,\n 
    "year": 2016,\n 
    "month": 9,\n 
    "opened": 1,\n 
    "opted_out": 0,\n 
    "sent": 1,\n 
    "signed_up": 0,\n 
    },\n 
    {\n 
    "count_purchased": 0,\n 
    "delivered": 56,\n 
    "clicked_unique": 0,\n 
    "shared": 0,\n 
    "mailings": 31,\n 
    "year": 2016,\n 
    "month": 9,\n 
    "opened": 1,\n 
    "opted_out": 0,\n 
    "sent": 102,\n 
    "signed_up": 0,\n 
    }\n 
] 
+0

:私たちはそれを道にアクセスする前に、いくつかの作業を行う必要があるので、

あなたの応答は、あなたがそれをしたい、複数のオブジェクトを返しますか? –

答えて

2

説明を少ししてanswer of M. I.の強化:

あなたが応答としてJSON文字列を取得しているので、あなたはそれを変換する必要があります。好都合なことに、PHPには、とりわけjson_decodeという機能があります。

あなたの応答が$mailに格納されている場合は、associative arrayまたは\stdClassのオブジェクトに変換するだけで済みます。それはJSON応答である

// Given the content of mail is your given json string 

// The second parameter allows us to use each entry of $mailData as \stdClass. 
// If you want to use an assiocative array instead, you can put in true for the second parameter. 
$mailData = json_decode($mail, false); // false can also be omitted in this case. 
echo $mailData[0]->sent; // 1 
echo $mailData[1]->sent; // 102 

// Now you are able to do fancy stuff with the data, for example loop over it. 
foreach($mailData as $singleMailData) { 
    // Do whatever you want with each entry. In my example I just print out the data. 
    var_dump($singleMailData); 
} 
+0

今後の使用のためによく理解されている説明にも、ありがとう! – SamXronn

1

あなたが応答としてJSONを得ている - 私はそれがこれはサンプルデータが返され$mail->delivered

ようなものになるだろう例えば配信アクセスするために探しています。使用:

json_decode($jsonString); // to get an `JSON` object or 
json_decode($jsonString, true); // to get an associative array. 
関連する問題