Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get value of object array

I have the following json:

$data = '{"code":"08261",
          "currency":"EUR", 
          "packs":[ {"amount":0.05,"measure":"g","price":73.0}, 
                    {"amount":0.1,"measure":"g","price":108.0}, 
                    {"amount":0.25,"measure":"g","price":154.0}, 
                    {"amount":0.5,"measure":"g","price":296.0}, 
                    {"amount":1.0,"measure":"g","price":394.0}, 
                    {"amount":2.5,"measure":"g","price":771.0}, 
                    {"amount":5.0,"measure":"g","price":1142.0}, 
                    {"amount":10.0,"measure":"g","price":1693.0}]}'; 

I can get the value of code and currency as follows:

// Option 1: through the use of an array.
$jsonArray = json_decode($data,true);

$code =  $jsonArray['code'];

// Option 2: through the use of an object.
$jsonObj = json_decode($data);

$code = $jsonObj->code;

How can I get the price for the following packs where the:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. amount is ‘1.0’ and measure is ‘g’
  2. amount is ‘5.0’ and measure is ‘g’
  3. amount is ‘10.0’ and measure is ‘g’

>Solution :

If you convert the json into nested arrays (passing true to the $associative parameter of json_decode, you can then use array_filter to filter the packs to find the values you want:

$data = '{"code":"08261",
          "currency":"EUR", 
          "packs":[ {"amount":0.05,"measure":"g","price":73.0}, 
                    {"amount":0.1,"measure":"g","price":108.0}, 
                    {"amount":0.25,"measure":"g","price":154.0}, 
                    {"amount":0.5,"measure":"g","price":296.0}, 
                    {"amount":1.0,"measure":"g","price":394.0}, 
                    {"amount":2.5,"measure":"g","price":771.0}, 
                    {"amount":5.0,"measure":"g","price":1142.0}, 
                    {"amount":10.0,"measure":"g","price":1693.0}]}'; 

function get_price($data, $amount, $measure) {
    $values = array_filter($data['packs'], function ($a) use ($measure, $amount) {
        return $a['amount'] == $amount && $a['measure'] == $measure;
    });
    if (count($values)) return reset($values)['price'];
    return 0;
}

$data = json_decode($data, true);

echo get_price($data, 1.0, 'g') . PHP_EOL;
echo get_price($data, 5.0, 'g') . PHP_EOL;
echo get_price($data, 10.0, 'g') . PHP_EOL;

Output:

394
1142
1693
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading