-2

I have an url that gives me this result:

{"status":"ok","result":[{"account":"11754-101","name":"test","channels":"15","billing":"1","billingstep":"60","increment":"0","credit":"1.0089"},{"account":"11754-102","name":"mult","channels":"15","billing":"1","billingstep":"60","increment":"0","credit":"2.7835"}]}

I'm trying to get the "credit" value of just one account to view it.

Account: 11754-102
Credit: XXX

The code I did is this, but it seems to be broken:

<?php
$json = '{"status":"ok","result":[{"account":"11754-101","name":"test","channels":"15","billing":"1","billingstep":"60","increment":"0","credit":"1.0089"},{"account":"11754-102","name":"mult","channels":"15","billing":"1","billingstep":"60","increment":"0","credit":"2.7835"}]}';

$apiResult = json_decode($json, true);
($apiResult['status'] !== 'ok') && 
  trigger_error('Unexpected API Result');

empty(($account = array_filter($apiResult['result'], function($item) {
  return $item['account'] === '11754-102';
}))) && trigger_error('Account not found.');

echo $account[0]['credit'];
?>

Can you help me?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Albert
  • 19
  • 3

3 Answers3

2

Not a huge fan of using the shortcuts you are using when a simple if() would do, but the main problem is that when you get to the end, you use

echo $account[0]['credit'];

If you look at the output of your array_filter(), in this case you end up with...

Array
(
    [1] => Array
        (
            [account] => 11754-102
            [name] => mult
            [channels] => 15
            [billing] => 1
            [billingstep] => 60
            [increment] => 0
            [credit] => 2.7835
        )

)

So although it only has one element, it is element 1 and not 0.

I've rewritten the code to take that into accounf (and added some ifs)...

$json = '{"status":"ok","result":[{"account":"11754-101","name":"test","channels":"15","billing":"1","billingstep":"60","increment":"0","credit":"1.0089"},{"account":"11754-102","name":"mult","channels":"15","billing":"1","billingstep":"60","increment":"0","credit":"2.7835"}]}';

$accountToFind = '11754-102';

$apiResult = json_decode($json, true);
if ($apiResult['status'] !== 'ok')  {
    trigger_error('Unexpected API Result');
}

$account = array_filter($apiResult['result'], 
    // Pass the account to find into the function rather than hardcode it.
    function($item) use ($accountToFind) {
        return $item['account'] === $accountToFind;
});

if ( !empty($account) ) {
    // If there is anything left, extract the first one
    $account = array_shift($account);
    echo $account['credit'];
}
else {
    trigger_error('Account not found.');
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

Since I don't know if you got an error message, if you want to display errors, you can use this article to do so: How do I get PHP errors to display?

The problem is that $account[0] doesn't exist, $account["result"][0] does exist. So the code would be echo $account['result'][0]['credit'];

Tim
  • 551
  • 3
  • 23
0

Here is your corrected code. You can achieve the same using array_pop. Your account was at offset 1 but you were fetching at 0.

<?php
$json = '{"status":"ok","result":[{"account":"11754-101","name":"test","channels":"15","billing":"1","billingstep":"60","increment":"0","credit":"1.0089"},{"account":"11754-102","name":"mult","channels":"15","billing":"1","billingstep":"60","increment":"0","credit":"2.7835"}]}';

$apiResult = json_decode($json, true);
($apiResult['status'] !== 'ok') &&
trigger_error('Unexpected API Result');

empty(($account = array_filter($apiResult['result'], function($item) {
    return $item['account'] === '11754-102';
}))) && trigger_error('Account not found.');

echo array_pop( $account )['credit'];
?>