0

The following is coded in the Laravel framework:

QUERY

$shipping = DB::table('shipping')->where('country',$delivery->country)->first();
$data= json_decode(json_encode($shipping),true);

RESULT

array (
  'id' => 3,
  'carrier' => 'EN',
  'country' => 'AU',
  'rates_json' => '{"rates": [{"international": [{"zone4": [{"to_kg": "2", "total": "1", "from_kg": "1"}, {"to_kg": "4", "total": "2", "from_kg": "3"}]}]}]}',
)  

In the MySQL database, I stored "rates_json" in a "JSON" datatype column. The attribute "from_kg" and "to_kg" is a range.

I intend to retrieve the total if a value is between the range. For instance, if value 1.5 is between 1 and 2 then the total is 1.

Your help is appreciated.

Thank You.

Eugene Anthony
  • 49
  • 1
  • 1
  • 11
  • Don't use decode of encoded. You have toArray function. Check [this](https://stackoverflow.com/questions/30411210/how-to-search-json-data-in-mysql) answer and use another where closure in first statement. – Tpojka Oct 07 '18 at 16:21

1 Answers1

2

Since your question is not clear,
I can only give you a hint, what you can do.

foreach (json_decode($data['rates_json'])->rates as $rates) {
    foreach ($rates->international as $international) {
        foreach ($international->zone4 as $zone) {
            if ($zone->from_kg <= $zone->total &&  $zone->total <= $zone->to_kg) 
            {
                // do whatever you want here.
                dump($zone->total);
            }
        }
    }
}

Let me know for any adjustments.

Tharaka Dilshan
  • 4,371
  • 3
  • 14
  • 28
  • Yes.This is the solution that I was seeking and I am making the adjustment. – Eugene Anthony Oct 07 '18 at 16:24
  • I replaced "$zone->total" to "$weight" where "$weight" is the value I provide. Now i'm trying to figure out let's say the maximum weight limit for a parcel is 30kg but my parcel is more than 30kg then how do I calculate the value for remaining kg then sum them up. – Eugene Anthony Oct 12 '18 at 17:13