How to correct PHP Notice: Undefined index

2.2k Views Asked by At

I getting error

PHP Notice: Undefined index: price in /home/***/public_html/catalog/view/theme/default/template/checkout/cart.tpl on line 57

What do I have to look up to correct this error. Thank you

54  <div>       
55  <?php foreach ($product['option'] as $option) { ?>       
56  <?php $option_table[$option['name']] = $option['value'];  ?>  
**57 <?php $option_table_price[$option['price']] = $option['price']; ?>**  
58  <?php if($option['name'][0] != 's') { ?>  
59  - <small><?php echo $option['name']; ?>: <?php echo $option['value']; ?></small><br />
60 <?php }?> 
Thank you 
61 <?php } ?>

1

There are 1 best solutions below

0
On

First way is check your array using isset function

 if (isset($option['type']))
 {
     $option_table_price[$option['type']] = $option['type'];
 }  

Another way is array_key_exists

 if (array_key_exists('type', $option))
 {
     $option_table_price[$option['type']] = $option['type'];
 }  

As for me, the second ways is better

Remark: Your code is unreadable, remove the extra PHP tags and make a single block of PHP code

<?php
foreach ($product['option'] as $option) {
    $option_table[$option['name']] = $option['value'];
    $option_table_price[$option['price']] = $option['price'];

    if($option['name'][0] != 's')
    {
        printf('<small>%s: %s</small><br />', $option['name'], $option['value']);
    }
}
?>