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

时间:2019-06-28 04:14:46

标签: php ajax

我想打印“ 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"
    }

 }

2 个答案:

答案 0 :(得分:1)

您需要使用带有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);
    }
}

Demo

答案 1 :(得分:0)

您可以像这样使用extract()函数

\extract($data, EXTR_OVERWRITE);
include $file;