-1

I am banging my head against a wall trying to figure out this error im getting in laravel (version 5.6.23, I checked :( ). Basically I'm reading an associative array created by JSONdecode, and one call is returning the error:

Undefined index : Itemprice  //this is the index name

But I've dd()'d the entire JSON to the console and that index certainly exists, not to mention it was working earlier today. Additionally, running isset() or array_key_exists both returns true for that exact index so even the code itself agrees it's an index that exists. I have no idea what is causing the error or where to go from here.

HEre's the code:

 foreach($items['ListOrderItemsResult']['OrderItems'] as $item)
    {
    //this is the problem line below
    $price = $item['ItemPrice']['Amount']; //ItemPrice is problem

    $productname = $item['Title']
    $quantity = $item['QuantityOrdered']; 
    $asin = $item['ASIN'];

    $total = $price * $quantity;
    }

and here is a dd of the actual array (above it is "$item") with some personal details removed

   array:11 [▼
  "QuantityOrdered" => "5"
  "Title" => "Two Pack...."
  "PromotionDiscount" => array:2 
   "IsGift" => "false"
   "ASIN" => "..."
  "SellerSKU" => "..."
  "OrderItemId" => "..."
  "ProductInfo" => array:1 
  "QuantityShipped" => "5"
   "ItemPrice" => array:2 [▼
  "CurrencyCode" => "USD"
   "Amount" => "30000"
   ]
   "ItemTax" => array:2 [▶]
  ]
  • Maybe the first `$item` has this index but some of the next from the array `$items['ListOrderItemsResult']['OrderItems']` don't have this index? – thefallen Jun 04 '18 at 09:25
  • Are you sure that all of the items in your collection contain this index? – Joe Jun 04 '18 at 09:26
  • `$item['ItemPrice']['Amount'];` from to `$item['ItemPrice'][0]['Amount'];` `itemPrice` is itself an array, so you cannot access it directly without its index. – arun Jun 04 '18 at 09:30

1 Answers1

0

The error states that Itemprice isn't a valid index. In your code example you write ItemPrice but the error is referencing a place where you didn't, double check your code and take notice that indexing is case sensitive.

The error is unlikely to be from the place you mentioned, or the index isn't defined in a later part of your array.

If you aren't sure if a variable is set, I suggest doing something like the following

$price = isset($item['ItemPrice']['Amount']) ? $item['ItemPrice']['Amount'] : null;

or shorthand syntax

$price = $item['ItemPrice']['Amount'] ?? null; 
Nicklas Kevin Frank
  • 6,079
  • 4
  • 38
  • 63
  • Sorry about that lack of capitalization, that was just a late night typo. The real error reads Undefined index : ItemPrice as well. – Matt Sciamanna Jun 04 '18 at 16:04
  • I't's also highlighting the ItemPrice line shown in the code as the place where the error comes from so it's definitely not a capitalization thing. – Matt Sciamanna Jun 04 '18 at 16:05
  • @MattSciamanna have you tried adding the code above? And have you looked through the rest of your ```OrderItems``` array to see if ALL of them contains ```ItemPrice```? – Nicklas Kevin Frank Jun 04 '18 at 16:20
  • Yes sorry for the delayed response, the code above is simply setting them to null, all of the items do contain ItemPrice, but none of them are showing as able to be accessed in the index unfortunately. – Matt Sciamanna Jun 04 '18 at 18:25
  • Setting them to null works to allow the rest of the code to run obviously, but doesnt work in that I still want the value contained within, which i can see with a simple DD() – Matt Sciamanna Jun 04 '18 at 18:25
  • This is just not possible. The pasted array cannot be what you are posting, given the error. Please double check your code. – Nicklas Kevin Frank Jun 04 '18 at 18:29
  • Exactly! Because if it's not ticking isset in your first code line, why is it doing it when i pull out to an above line and check if it isset. It's something weird with laravel. But I exchanged some of the logic between the controller and model and that managed to get rid of the errror, even though the code wasn't changing. Not sure what was causing that in the framework. – Matt Sciamanna Jun 04 '18 at 20:30