0

我可能有以下array()

Array([
    0
]=>Array([
    part_id
]=>6[
    part_user_id
]=>2[
    part_department
]=>3[
    part_category
]=>1[
    part_subcategory
]=>1[
    part_name
]=>SparePart[
    part_qty
]=>1[
    part_condition
]=>New[
    part_description
]=>Thisisadescription[
    part_image
]=>images/parts/default.jpg)[
    1
]=>Array([
    part_id
]=>7[
    part_user_id
]=>2[
    part_department
]=>3[
    part_category
]=>1[
    part_subcategory
]=>1[
    part_name
]=>SparePart[
    part_qty
]=>1[
    part_condition
]=>New[
    part_description
]=>Thisisadescription[
    part_image
]=>images/parts/default.jpg))

从我从 MYSQL 获得的数据中,相同的查询也可能返回不止一行,所以我也可以收到这个array()

Array([
    part_id
]=>7[
    part_user_id
]=>2[
    part_department
]=>3[
    part_category
]=>1[
    part_subcategory
]=>1[
    part_name
]=>SparePart[
    part_qty
]=>1[
    part_condition
]=>New[
    part_description
]=>Thisisadescription[
    part_image
]=>images/parts/default.jpg)

我正在循环输出我的 HTML

foreach($myResults as $row => $value){

}

当我从数据库收到超过 1 行时,此循环将正常工作。如果我收到 1 行,我显然会遇到问题。

我检查收到的内容以便循环或简单地输出取决于我的结果的最佳方法是什么?

4

1 回答 1

0

检查 value 是否为数组。如果是,也循环它。

if (is_array($value)) foreach ($value as $value2) {}
于 2013-07-17T16:16:58.603 回答