вывод json преобразовать в строку php

Ниже мой рабочий API, но результат для "echo $data" только "array" и для "echo $data["data"]["operatorid"]" ничего не экран гаснет. Так как конвертировать json reponse в php строка, а также получить конкретные значения из того же

$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 ответ:

{
"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"}
]

1

Решение

$data содержит многомерный массив — «данные», который сам содержит несколько массивов.

echo $data["data"]["operatorid"] не вернет ничего, потому что вы не указываете, какой из этих массивов вы хотите напечатать operatorid от.

Либо укажите, на какой массив вы ориентируетесь, с помощью следующего

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

или перебрать ваш массив $ data так

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

Другие решения

Эта строка JSON преобразуется в объект PHP, имеющий свойство с именем data который является массивом объектов

Выход из 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
)

)

)

Поэтому для ссылки на отдельный элемент вам нужно использовать этот синтаксис

$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;

Это может быть легче понять, если вы используете имя переменной, отличное от $data так

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

Или если вы обрабатываете все происшествия

$jsonObj = (json_decode($res));
foreach ($jsonObj -> data as $data) {
echo $data -> operatorid;
}
1

По вопросам рекламы [email protected]