1

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"
}
]
Serving Quarantine period
  • 66,345
  • 10
  • 43
  • 85
user3588059
  • 71
  • 1
  • 2
  • 9

2 Answers2

1

That JSON string converts to a PHP object that has a property called data which is an array of objects

Output from print_r()

stdClass Object
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [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
                )

            [1] => stdClass Object
                (
                    [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
                )

            [2] => stdClass Object
                (
                    [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
                )

            [3] => stdClass Object
                (
                    [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
                )

            [4] => stdClass Object
                (
                    [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
                )

            [5] => stdClass Object
                (
                    [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
                )

            [6] => stdClass Object
                (
                    [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
                )

        )

)

So to reference an individual item you would need to use this syntax

$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));
echo $data->data[0]->operatorid;

It may be easier to understand if you use a variable name other than $data so

$jsonObj = (json_decode($res));
echo $jsonObj -> data[0] -> operatorid;

Or if you are processing all the occurances

$jsonObj = (json_decode($res));
foreach ($jsonObj -> data as $data) {
    echo $data -> operatorid;
}
RiggsFolly
  • 83,545
  • 20
  • 96
  • 136
0

$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'];
    }
}
Simian
  • 652
  • 7
  • 19