Getting null value from an array even if there are values โ€‹โ€‹in the array

I have a simple ordering system. When an order is submitted, the customer should have a download button on their orders page. So far, so good. The order details are displayed except for one add value which I see in the array.

This is what the array looks like

object(stdClass)#454 (3) {
  ["223"]=>
  object(stdClass)#453 (3) {
    ["title"]=>
    string(7) "test"
    ["price"]=>
    int(12)
    ["file_id"]=>
    int(2)
  }
  ["224"]=>
  object(stdClass)#451 (3) {
    ["title"]=>
    string(13) "test2"
    ["price"]=>
    int(1)
    ["file_id"]=>
    int(3)
  }
  ["shipping"]=>
  object(stdClass)#456 (2) {
    ["title"]=>
    string(17) "Shipping Option 1"
    ["price"]=>
    int(10)
  }
}

      

Objects 223

and 224

are the identifier of two products. On the page, I can see title

, and price

for both products, but file_id

for them to NULL. This is how I show them

 @foreach($download->getOrderData($download->order_details) as $itemId => $item)
     <p>{{ $item->title }}</p>
     <p>{{ $item->price }}</p>
     <p>{{ $item->file_id }}</p>
@endforeach

      

This is the order model

public function getOrderData($data)
{
    $dataArray = json_decode($data, true);
    $arrayKeys = array_keys($dataArray);
    $arrayKeys = array_filter($arrayKeys, function ($value) {
        return ($value !== 'shipping');
    });
    $productIds = implode(',', $arrayKeys);

    $products = DB::table('products')
        ->leftjoin('category', 'products.category_id', '=', 'category.category_id')
        ->leftjoin('sub_category', 'products.sub_cat_id', '=', 'sub_category.sub_cat_id')
        ->where('product_id', $productIds)
        ->get();

    foreach ($products as $item) {
        if (!in_array($item->product_id, $arrayKeys)) continue;
        $dataArray[$item->product_id]['category'] = $item->category_name;
        $dataArray[$item->product_id]['sub_category'] = $item->sub_cat_name;

    }
    return json_decode(json_encode($dataArray));
}

      

+3


source to share


1 answer


I suspect the underscore is causing problems. https://twig.symfony.com/doc/2.x/templates.html#variables

Hence, it is possible



{{ $item['file_id'] }}

      

You can do the trick.

0


source







All Articles