我有以下输出为json。我怎样才能回显它的DND_status?以下是代码。
JSON OUTPUT:
Array ( [0] => Array ( [mobilenumber] => 9809657248 [DND_status] => on [activation_date] => 09-10-2014 17:23 [current_preference] => 0 [preference_history] => Array ( [0] => Array ( [preference_date] => 20-08-2012 [preference] => 0 [status] => Active ) [1] => Array ( [preference_date] => 13-08-2012 [preference] => 0 [status] => Active ) [2] => Array ( [preference_date] => 17-07-2012 [preference] => 0 [status] => Active ) [3] => Array ( [preference_date] => 16-07-2012 [preference] => 0 [status] => Active ) [4] => Array ( [preference_date] => 09-07-2012 [preference] => 0 [status] => Active ) ) ) )
PHP代码:
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Mashape-Key: xd7lpM6y3ImshZOdNfobRDPhiBU4p100Q2JjsnaAlOTxVWrkwZ"
)
);
$context = stream_context_create($opts);
$res = file_get_contents('https://dndcheck.p.mashape.com/index.php?mobilenos=9809657248', false, $context);
$res = (json_decode($res, true));
print_r ($res); // print the above json
echo $res['DND_status']; //not working
echo $res->DND_status; //not working
?>
答案 0 :(得分:2)
直接阅读数组:
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Mashape-Key: xd7lpM6y3ImshZOdNfobRDPhiBU4p100Q2JjsnaAlOTxVWrkwZ"
)
);
$context = stream_context_create($opts);
$res = file_get_contents('https://dndcheck.p.mashape.com/index.php?mobilenos=9809657248', false, $context);
$res = (json_decode($res, true));
print_r ($res); // print the above json
echo $res[0]['DND_status']; // what you need by reading in array
?>
或使用foreach循环:
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Mashape-Key: xd7lpM6y3ImshZOdNfobRDPhiBU4p100Q2JjsnaAlOTxVWrkwZ"
)
);
$context = stream_context_create($opts);
$res = file_get_contents('https://dndcheck.p.mashape.com/index.php?mobilenos=9809657248', false, $context);
$res = (json_decode($res, true));
print_r ($res); // print the above json
foreach ($res as $key => $value) {
echo $value['DND_status']; // what you need by foreach method
}
?>
答案 1 :(得分:0)
试试这应该有效:
echo $res[0]['DND_status'];
答案 2 :(得分:-1)
如评论中所述,使用foreach循环
foreach ($opts as $opt) {
foreach ($opt as $op) {
echo $op['method'];
echo $op['header'];
}
}