【发布时间】:2012-09-09 12:19:59
【问题描述】:
我有一个数组,一个例子是..
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)
}
它们似乎都是有效的资源,我已检查密钥名称是否正确。
【问题讨论】:
-
您知道没有第二个参数的
mysql_fetch_array()将返回 数字键和关联键,对吧?所以list()不会得到你期望的值。而是调用mysql_fetch_array($sql['cats'], MYSQL_ASSOC)或对这两个循环使用mysql_fetch_assoc()。 -
$sql['listings'][$main_category_slug]中有什么内容?真的是mysql结果资源吗? -
检查您是否没有在某处调用 mysql_free_result() 来获取您的资源...另外,您是否可以包含 mysql_fetch_array($sql['listings'][$main_category_slug]) 部分的转储?
-
@MichaelBerkowski 是的,我知道它会同时返回,我并不真的需要两者,继续使用它只是我的一个坏习惯。但是,我过去从未遇到过这样的问题。
-
@MichaelBerkowski 正如您在上面的示例数组输出中看到的那样,
$sql['listings'][$main_category_slug]是一个 MySQL 资源。$main_category_slug等于其中一个名称。
标签: php mysql arrays multidimensional-array