Reputation:
I have got the following output as json. How can i echo only DND_status from it? Following are the codes.
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 CODE:
<?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
?>
Upvotes: 0
Views: 88
Reputation:
As mentioned in the comment, use foreach loop
foreach ($opts as $opt) {
foreach ($opt as $op) {
echo $op['method'];
echo $op['header'];
}
}
Upvotes: -1
Reputation: 775
Directly by reading the array :
<?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
?>
Or with foreach loop :
<?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
}
?>
Upvotes: 2