首页 > 解决方案 > PHP check JSON string for item = value

问题描述

I need to get the value one one particular value in my json string

the json string comes from an api url http://url:port/api.php?action=reg_user&sub=list and outputs like so

[{"id":"1","username":"username1","credits":"0","group_id":"1","group_name":"Administrators","last_login":"1511883014","date_registered":"1421604973","email":"test1@my-email.com","ip":"8.8.8.8","status":"1"},{"id":"31","username":"username2","credits":"0","group_id":"1","group_name":"Administrators","last_login":"1539813642","date_registered":"1473632400","email":"test2@my-email.com","ip":"8.8.8.8","status":"1"},

i would like to check the value of credits where username = username1

then if credits > 30 do x, else do y

I presume first of all I need to decode this jason string

so i tried to do

$api_result = file_get_contents( 'http://url:port/api.php?action=reg_user&sub=list' );
$json_decode = json_decode($api_result);
echo $json_decode;

expecting this to echo the array in a nicely formatted structure, instead it just outputs the word Array. Where do I go from here?

Thanks

UPDATE 1...

SO i checked again and I defo can echo $api_result so I know that the file_get_contents is working fine i tried the two comments suggestions however both didn't work...

one of the suggestions was to make my php $api_result = file_get_contents( 'http://url:port/api.php?action=reg_user&sub=list' ); $json_decode = json_decode($api_result); echo $json_decode['username'];

here is a screenshot of how the echo $api_result is formatted in case this isnt a propper json string

edit 1 - echo of $api_result

so to me this looks like it opens with (and is all contained in a pair of [] and then each result is enclosed in {} as I'd expect with a json string right? however i thought json strings used {} and arrays used {} so to me this looks like a string inside an array???

I looked at php.net's resource on JSON DECODE and tried var_dump(json_decode($json)); which did print me this

enter image description here

UPDATE 2

I just tried https://www.functions-online.com/json_decode.html and pasted the data in, it was able to decode the data absolutely fine,

It then gave me a copy if the json_decode sample at the bottom but this didn't work either, returning a null value like json_decode doesn't work on my server?

标签: phparraysjson

解决方案


From your second screenshot (please cut & paste the text instead!) you can see the decoded JSON is an array of objects with properties id, username etc. So you can access them using object notation. This code uses the snippet of your api_result from your original question to demonstrate how to do what you want:

$api_result = '[{"id":"1","username":"username1","credits":"0","group_id":"1","group_name":"Administrators","last_login":"1511883014","date_registered":"1421604973","email":"test1@my-email.com","ip":"8.8.8.8","status":"1"},{"id":"31","username":"username2","credits":"0","group_id":"1","group_name":"Administrators","last_login":"1539813642","date_registered":"1473632400","email":"test2@my-email.com","ip":"8.8.8.8","status":"1"}]';
$json = json_decode($api_result);
foreach ($json as $user) {
    if ($user->username == 'username1') {
        if ($user->credits > 30) {
            // do x
            echo "user $user->username has more than 30 credits ($user->credits)";
        }
        else {
             // do y
            echo "user $user->username has less than or equal to 30 credits ($user->credits)";
        }
    }
}

Output:

user username1 has less than or equal to 30 credits (0)

推荐阅读