phpjsonmashup

json output convert to php string


Below is my working api but result for "echo $data" is only "array" and for "echo $data["data"]["operatorid"]" is nothing screen goes blank. So how to convert json reponse to php string and also get particular values from same

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Mashape-Key: XXXXXXXX"               
)
);

$context = stream_context_create($opts);
$res = file_get_contents('https://tariff-plan-api-datayuge.p.mashape.com/index.php?circleid=kerala&limit=50&operatorid=BSNL&recharge_type=top', false, $context);
$data = (json_decode($res, true));
echo $data["data"]["operatorid"];

json response :

{
"data": [
{
  "id": "3148",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "150",
  "recharge_talktime": "150",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 150 By BSNL",
  "recharge_longdesc": "Full Talktime (offer valid upto 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3149",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "250",
  "recharge_talktime": "250",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 250 By BSNL",
  "recharge_longdesc": "Full Talktime (offer valid upto 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3150",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "550",
  "recharge_talktime": "550",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 550 By BSNL",
  "recharge_longdesc": "Full Talktime (offer valid upto 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3151",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "1000",
  "recharge_talktime": "1100",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 1000 By BSNL",
  "recharge_longdesc": "Extra Talktime (offer is valid till 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3152",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "1100",
  "recharge_talktime": "1210",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 1100 By BSNL",
  "recharge_longdesc": "Extra Talktime (offer is valid till 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3153",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "1500",
  "recharge_talktime": "1650",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 1500 By BSNL",
  "recharge_longdesc": "Extra Talktime (offer is valid till 23 Mar 2015)",
  "recharge_type": "Full Talktime"
},
{
  "id": "3154",
  "operatorid": "BSNL",
  "circleid": "Kerala",
  "recharge_amount": "2000",
  "recharge_talktime": "2200",
  "recharge_validity": "Unlimited",
  "recharge_shortdesc": "Recharge Of Rs 2000 By BSNL",
  "recharge_longdesc": "Extra Talktime (offer is valid till 23 Mar 2015)",
  "recharge_type": "Full Talktime"
}
]

Solution

  • $data holds a multidimensional array - "data" which itself holds multiple arrays.

    echo $data["data"]["operatorid"] will not return anything because you are no specifying which of these arrays you want to print operatorid from.

    Either specify which array you are targeting with the following

    $data["data"][*index*]["operatorid"]

    or loop through your $data array like so

    foreach( $data as $Adata ){
        foreach( $Adata as $arr ){
            echo $arr['operatorid'];
        }
    }