How to correct PHP Notice: Undefined index

可紊 提交于 2020-01-06 03:43:05

问题


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:


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']);
    }
}
?>


来源:https://stackoverflow.com/questions/27033914/how-to-correct-php-notice-undefined-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!