-3

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?

Henry Aspden
  • 1,863
  • 3
  • 23
  • 45
  • If it **is** indeed an array, what you need to do is access it as you would access an array. E.g., `echo $json_decode['username']`. If you want to see the whole array at once, create a loop for it, or just dump the variable using `var_dump` – Wreigh Oct 18 '18 at 00:41
  • 2
    Use `print_r(json_decode($api_result));` to pretty print a structure. Run a loop over the decoded structure and check `$arr[i]['credits'] > 30`. – ggorlen Oct 18 '18 at 00:42
  • 1
    Possible duplicate of [Read and print json array in php](https://stackoverflow.com/questions/41277289/read-and-print-json-array-in-php) – Wreigh Oct 18 '18 at 00:45
  • thanks for your comment @Wreigh however unfortunately this echo'ed nothing. returned a blank screen – Henry Aspden Oct 18 '18 at 01:04
  • update your question to include what you've tried so we can help you further :) – Wreigh Oct 18 '18 at 01:05
  • thanks @Wreigh i have done... Please note the secion about it being formatted as [{string}] inside of square brackets ? – Henry Aspden Oct 18 '18 at 01:29

1 Answers1

1

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)
Nick
  • 138,499
  • 22
  • 57
  • 95
  • this works perfectly until I try and make $api_result = my original file_get_contents so is the issue I am having with my file_get_contents perhaps ??? could this be a character encoding issue? – Henry Aspden Oct 18 '18 at 01:58
  • comes back completely blank screen... presumably this is because it cant decode the info returned by the api... – Henry Aspden Oct 18 '18 at 02:00
  • sorry @Nick, my mistake I had made when typing back in the API URL... All sorted now this is perfect thank you !!! – Henry Aspden Oct 18 '18 at 02:03