如何获取“0”和“1”的值作为php页面的变量

我想打印“0”和“1”的值以打印在 php 页面上。为了做到这一点,我想知道如何以数组的形式访问这个 json 对象。我已经解码了。但仍然无法打印。


 {

    "statusCode": 200,

    "message": "Data Received",

    "0": {

        "id": "385",

        "fname": "arriet ",

        "lname": "ephania ",

        "email": "amym@mator.com",

        "mob": "9877"

    },

    "1": {

        "id": "386",

        "fname": "Kelly ",

        "lname": "Echo ",

        "email": "sacen@mailinator.com",

        "mob": "1111"

    }


 }


一只斗牛犬
浏览 114回答 2
2回答

慕慕森

您需要使用带有 true 标志的 json_decode 将其转换为数组,然后让每个数组循环遍历其中的数据,$arr = json_decode('    {    "statusCode": 200,    "message": "Data Received",    "0": {        "id": "385",        "fname": "arriet ",        "lname": "ephania ",        "email": "amym@mator.com",        "mob": "9877"    },    "1": {        "id": "386",        "fname": "Kelly ",        "lname": "Echo ",        "email": "sacen@mailinator.com",        "mob": "1111"    } }    ', true);foreach ($arr as $key => $value) {    // strict type check for key with integer values.    if(intval($key) === $key){        print_r($value);    }}

一只名叫tom的猫

你使用这样的extract()功能\extract($data, EXTR_OVERWRITE);include $file;
打开App,查看更多内容
随时随地看视频慕课网APP