注意:未定义的索引:foreach循环php中的名称

时间:2015-12-03 13:01:28

标签: php arrays foreach

我有两个数组存储在单个变量中我希望通过for-each循环检索数据,问题是name索引给出了错误。请查看以下代码。

阵列

Array
(
    [name] =>  special
    [Cat_id] => 59
)
 Array
(
    [singleproduct] => Array
        (
            [product_id] => 52
            [thumb] => http://localhost/swefloraProject/upload/image/cache/catalog/birthday/home-product-01-80x80.png
            [name] => gift flowers
            [description] => when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has ..
            [price] => $0.00
            [special] => 
            [tax] => $0.00
            [minimum] => 1
            [rating] => 0
            [href] => http://localhost/Project/upload/index.php?route=product/product&product_id=52
        )

)

这是循环

foreach($categories as $category){ 
print_r($category['name']);
}

错误

Notice: Undefined index: name in

1 个答案:

答案 0 :(得分:0)

foreach($categories as $key=> $category){ 
   print_r($key); // prints "name" on first iteration
   print_r($category); // prints "special" on first iteration
}