我有一个数组,一个例子是..
Array
(
[cats] => Resource id #54
[listings] => Array
(
[home-and-garden] => Resource id #55
[professional-services] => Resource id #56
[community] => Resource id #57
[education-and-instruction] => Resource id #58
[automotive] => Resource id #59
[legal-and-financial] => Resource id #60
)
)
现在,cats
键是一个 MySQL 数据集,我使用 mysql_fetch_array
循环访问该数据集没有任何问题;然而,一旦进入该循环,我尝试在列表数组的某个键上运行另一个循环,例如 home-and-garden,列表数组下的所有键都是动态的,所以我必须通过在具有键名称的变量中,但它不会进入循环。
下面是我的代码示例..
protected function makePopularCategoryHTML($sql) {
while (list($main_category,$slug,$image)=mysql_fetch_array($sql['cats'])) {
// Make lowercase category slug
$main_category_slug = URLSafe($main_category);
while (list($category,$name,$tag1,$newurl)=mysql_fetch_array($sql['listings'][$main_category_slug])) {
// It won't enter this loop
}
}
}
编辑:示例 $sql['listings'][$main_category_slug]
的转储如下:
resource(55) of type (mysql result)
$sql['listings']
的转储如下:
array(6) {
["professional-services"]=>
resource(55) of type (mysql result)
["home-and-garden"]=>
resource(56) of type (mysql result)
["community"]=>
resource(57) of type (mysql result)
["food-and-dining"]=>
resource(58) of type (mysql result)
["real-estate"]=>
resource(59) of type (mysql result)
["business-to-business"]=>
resource(60) of type (mysql result)
}
它们似乎都是有效的资源,并且我已检查 key 名称是否正确。
最佳答案
它不会进入的数组是空的,因为我假设由于我没有从相应的查询中收到任何错误,所以查询是正常的,但事实并非如此 - 其中一个搜索参数为空。
如果您遇到这样的问题,始终输出相应的查询并手动运行它以查看是否有空结果集。
关于php - 循环遍历动态 mysql 数据数组时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338997/