0

I have 3 tables

Product(id, sku, product_name) 
product_sales_forecast_3(id,product_id)
product_sales_forecast_detail_3(id,product_sales_forecast,forecast,year)

forecast field in table product_sales_forecast_detail_3 is a JSON, it looks like:

{"25":{"qty":0},"26":{"qty":0},"27":{"qty":0},"28":{"qty":0},"29":{"qty":0},"30":{"qty":1},"31":{"qty":0},"32":{"qty":0},"33":{"qty":1},"34":{"qty":1},"35":{"qty":0},"36":{"qty":0},"37":{"qty":0},"38":{"qty":0},"39":{"qty":0},"40":{"qty":0},"41":{"qty":0},"42":{"qty":0},"43":{"qty":0},"47":{"qty":0},"48":{"qty":0},"49":{"qty":0},"50":{"qty":0},"51":{"qty":0},"52":{"qty":0}}

In laravel controller I wrote:

 $DataForecasts=DB::table('product_sales_forecast_3')
->join('product_sales_forecast_detail_3','product_sales_forecast_3.id',
"=",'product_sales_forecast_detail_3.product_sales_forecast_id')
->join('products','products.id','=','product_sales_forecast_3.product_id')
->select('product_sales_forecast_detail_3.forecast')
->where([
        ['product_sales_forecast_3.channel','=', $AVCWHStoreID],
        ['products.product_sku','=', $sku],
        ['product_sales_forecast_detail_3.year','=', 2019]
        ])->get();

        $objs = json_decode($DataForecasts);
       foreach($objs as  $obj)
       {
        dd($obj);
       }

The result is:

{#1457 ▼ +"forecast": "{"1":{"qty":145},"2":{"qty":0},"3":{"qty":0},"4":{"qty":0},"5":{"qty":0},"6":{"qty":0},"7":{"qty":58},"8":{"qty":69},"9":{"qty":74},"10":{"qty":97},"11":{"qty":69},"12":{"qty":69},"13":{"qty":74},"14":{"qty":95},"15":{"qty":94},"16":{"qty":93},"17":{"qty":87},"18":{"qty":85},"19":{"qty":79},"20":{"qty":80},"21":{"qty":77},"22":{"qty":64},"23":{"qty":68},"24":{"qty":63},"25":{"qty":63},"26":{"qty":60},"27":{"qty":70},"28":{"qty":83},"29":{"qty":99},"30":{"qty":86},"31":{"qty":29},"32":{"qty":68},"33":{"qty":68},"34":{"qty":62},"35":{"qty":63},"36":{"qty":66},"37":{"qty":66},"38":{"qty":65},"39":{"qty":55},"40":{"qty":39},"41":{"qty":63},"42":{"qty":52},"43":{"qty":52},"44":{"qty":47},"45":{"qty":64},"46":{"qty":74},"47":{"qty":80},"48":{"qty":105},"49":{"qty":102},"50":{"qty":106},"51":{"qty":88},"52":{"qty":68}} ◀"}

I try to access a value but I don't know how to access the values.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

3 Answers3

2

Try this ...

<?php

$objs = '{ 
    "forecast": 
    {
        "1":{"qty":145}
        ,"2":{"qty":0}
        ,"3":{"qty":0}
        ,"4":{"qty":0}
        ,"5":{"qty":0}
        ,"6":{"qty":0}
        ,"7":{"qty":58}
        ,"8":{"qty":69}
        ,"9":{"qty":74}
        ,"10":{"qty":97}
        ,"11":{"qty":69}
        ,"12":{"qty":69}
        ,"13":{"qty":74}
        ,"14":{"qty":95}
        ,"15":{"qty":94}
        ,"16":{"qty":93}
        ,"17":{"qty":87}
        ,"18":{"qty":85}
        ,"19":{"qty":79}
        ,"20":{"qty":80}
        ,"21":{"qty":77}
        ,"22":{"qty":64}
        ,"23":{"qty":68}
        ,"24":{"qty":63}
        ,"25":{"qty":63}
        ,"26":{"qty":60}
        ,"27":{"qty":70}
        ,"28":{"qty":83}
        ,"29":{"qty":99}
        ,"30":{"qty":86}
        ,"31":{"qty":29}
        ,"32":{"qty":68}
        ,"33":{"qty":68}
        ,"34":{"qty":62}
        ,"35":{"qty":63}
        ,"36":{"qty":66}
        ,"37":{"qty":66}
        ,"38":{"qty":65}
        ,"39":{"qty":55}
        ,"40":{"qty":39}
        ,"41":{"qty":63}
        ,"42":{"qty":52}
        ,"43":{"qty":52}
        ,"44":{"qty":47}
        ,"45":{"qty":64}
        ,"46":{"qty":74}
        ,"47":{"qty":80}
        ,"48":{"qty":105}
        ,"49":{"qty":102}
        ,"50":{"qty":106}
        ,"51":{"qty":88}
        ,"52":{"qty":68} 
    }  
    }';
$objs = json_decode($objs);
foreach($objs->forecast as $key=>$val)
{
    //var_dump($key);  // $key will be week
    //var_dump ($val->qty); // qty here
}

If array , change foreach loop like following code ..

   foreach($objs['forecast'] as $key=>$val)
   {
      // var_dump($key);  // $key will be week
      // var_dump ($val['qty']); qty here
    }
Zar Ni Ko Ko
  • 352
  • 2
  • 7
  • Thank Sir. But my obj = array:1 [ "forecast" => "{"1":{"qty":1},"2":{"qty":1},"3":{"qty":1},"4":{"qty":1},"5":{"qty":0},"6":{"qty":1},"7":{"qty":0}} ]. Please help me how to remove "array:1 []" from the obj –  dang quang hai Dec 09 '19 at 06:28
  • check my answer again @dang quang hai – Zar Ni Ko Ko Dec 09 '19 at 06:37
  • Thank Sir, But I think there is a problem with my array. After line $objs = json_decode($DataForecasts,true); I try to print out the $objs . The result is Array ( [0] => Array ( [forecast] => {"1":{"qty":1},"2":{"qty":1},"3":{"qty":1},"4":{"qty":1},"5":{"qty":0},"6":{"qty":1},"7":{"qty":0} } ) ). please help me to foreach loop with kind of array. Thank so much –  dang quang hai Dec 09 '19 at 06:58
  • make again `$objs = json_decode($objs[0]['forecast'],true);` and show `var_dump($objs);` – Zar Ni Ko Ko Dec 09 '19 at 07:15
  • You can try with my second answer – Zar Ni Ko Ko Dec 09 '19 at 07:31
1

You can use json_decode, jms/serializer, or symfony/serializer.

metropolision
  • 405
  • 4
  • 10
1

Change in your controller like ...

$DataForecasts=DB::table('product_sales_forecast_3')
->join('product_sales_forecast_detail_3','product_sales_forecast_3.id',
"=",'product_sales_forecast_detail_3.product_sales_forecast_id')
->join('products','products.id','=','product_sales_forecast_3.product_id')
->select('product_sales_forecast_detail_3.forecast')
->where([
        ['product_sales_forecast_3.channel','=', $AVCWHStoreID],
        ['products.product_sku','=', $sku],
        ['product_sales_forecast_detail_3.year','=', 2019]
        ])->get()->toArray();

        $objs = json_decode($DataForecasts['forecast'],true);
        foreach($objs as  $key=>$val)
        {
            var_dump($key) // week here
            var_dump($val['qty'])
        }
Zar Ni Ko Ko
  • 352
  • 2
  • 7
  • Thank So much Sir ! But $objs = json_decode($objs[0]['forecast'],true); foreach($objs as $key=>$val) { print_r($key); // $key will be week print_r('
    '); print_r($val['qty']);// qty here } It works.
    –  dang quang hai Dec 09 '19 at 07:40