json 输出转换为 php 字符串

json output convert to php string

以下是我的工作 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"
}
]

$data 包含一个多维数组 - "data" 本身包含多个数组。

echo $data["data"]["operatorid"] 不会 return 任何东西,因为您没有指定要从中打印 operatorid 的数组。

使用以下内容指定您要定位的阵列

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

或者像这样遍历你的 $data 数组

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

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