2016-07-09 17 views
0

次のJSONデータはID番号があります。最後に:何ができるかように$ johnemail ==「[email protected]PHPでJSONデータの変数を読み込んで保存する

、ジョンの変数$ johnemailに格納します。

[{"Name":"malek","Email":"[email protected]","Donation":"5","Type":"Cash","id":0},{"Name":"donnie","Email":"[email protected]","Donation":"6","Type":"Cheque","id":1},{"Name":"ramanuj","Email":"[email protected]","Donation":"3","Type":"NEFT","id":2},{"Name":"neha","Email":"[email protected]","Donation":"2","Type":"RTGS","id":3},{"Name":"aasha","Email":"[email protected]","Donation":"1","Type":"CC","id":4},{"Name":"zia","Email":"[email protected]","Donation":"6","Type":"PayU","id":5},{"Name":"John","Email":"[email protected]","Donation":"7","Type":"Instamojo","id":6}] 

私は、特に電子メールを取得したいですそれを行う最も簡単な方法は?

+0

'json_decode'? – RomanPerekhrest

答えて

0

使用した簡単な解決策json_decode、およびarray_search機能(PHP 5.5以降で利用可能)array_column

$json_data = '[{"Name":"malek","Email":"[email protected]","Donation":"5","Type":"Cash","id":0},{"Name":"donnie","Email":"[email protected]","Donation":"6","Type":"Cheque","id":1},{"Name":"ramanuj","Email":"[email protected]","Donation":"3","Type":"NEFT","id":2},{"Name":"neha","Email":"[email protected]","Donation":"2","Type":"RTGS","id":3},{"Name":"aasha","Email":"[email protected]","Donation":"1","Type":"CC","id":4},{"Name":"zia","Email":"[email protected]","Donation":"6","Type":"PayU","id":5},{"Name":"John","Email":"[email protected]","Donation":"7","Type":"Instamojo","id":6}]'; 

$search_name = 'John'; 
$items = json_decode($json_data, TRUE); 
$emails = array_column($items, 'Name', 'Email'); // email is good for using as array key, as it should be unique 
$johnemail = array_search($search_name, $emails); // note that this is case-sensitive search 

print_r($johnemail); // "[email protected]" 
+0

@DonnieAshok、それはあなたのために動作する場合、あなたは答えを受け入ろうとしましたか? – RomanPerekhrest

+0

ごめんなさい申し訳ありませんでした!新しいstackoverflowに。ありがとう! –

+0

@DonnieAshok、それは大丈夫です – RomanPerekhrest

関連する問題