在PHP中检查json键的值

时间:2022-10-26 16:46:25

I have this function in php

我在php中有这个功能

<?php function showTable(){
$url = "http://10.0.0.1/lib/api/desk/branch/";

$params = array ("action" => "list","company_key" => "1");

$result=requestURL($url,$params);

$json_a=json_decode(strip_tags($result),true);

?>

This is the json I got from the code above. I need to check the key "status". If it has a value "no", it should display an alert that status is not ok. Thanks!

这是我从上面的代码得到的json。我需要检查关键“状态”。如果它的值为“no”,则应显示状态不正常的警报。谢谢!

{
    "init": [
        {
            "status": "ok",
            "record_count": 9,
            "code": "",
            "message": "",
            "page_count": null,
            "current_page": 0
        }
    ]
}

2 个解决方案

#1


1  

Easy.

Suppose you hold the JSON in $json.

假设你在$ json中持有JSON。

$jsonDecoded = json_decode($json, true);

if($jsonDecoded['init'][0]['status'] != 'ok') {
    print "status is not ok :(";
}

#2


0  

Let say that json is in $json variable

假设json在$ json变量中

    $json = json_decode($json,true); 
        $status = $json['init'][0]['status'];
        if($status=='ok'){
           # do something here
        }else{
           # display not ok here
        }

#1


1  

Easy.

Suppose you hold the JSON in $json.

假设你在$ json中持有JSON。

$jsonDecoded = json_decode($json, true);

if($jsonDecoded['init'][0]['status'] != 'ok') {
    print "status is not ok :(";
}

#2


0  

Let say that json is in $json variable

假设json在$ json变量中

    $json = json_decode($json,true); 
        $status = $json['init'][0]['status'];
        if($status=='ok'){
           # do something here
        }else{
           # display not ok here
        }