【发布时间】:2014-04-08 19:20:42
【问题描述】:
当我在没有 foreach 的情况下在 WordPress 中运行以下 PHP 时,我成功打印了一个多维数组。当我使用foreach 时,它会返回一个error 500,我只是想遍历结果,以便能够选择每个name,然后将其推送到另一个数组。
如果有人可以帮助我遍历这个数组,那就太好了!
Array
(
[0] => stdClass Object
(
[term_taxonomy_id] => 26
[taxonomy] => product_brand
[name] => Authentic Cheese
[slug] => authentic-cheese
)
[1] => stdClass Object
(
[term_taxonomy_id] => 27
[taxonomy] => product_brand
[name] => Robot
[slug] => robot
)
)
PHP
$q2 = "SELECT t.term_taxonomy_id, t.taxonomy, e.name, e.slug
FROM wp_term_taxonomy t
INNER JOIN wp_terms e ON t.term_taxonomy_id = e.term_id
WHERE taxonomy = 'product_brand'";
$r2 = $wpdb->get_results($q2);
print_r($r2);
foreach ($r2 as $row) {
echo $row['name'];
}
【问题讨论】:
-
那不是二维数组,是对象数组
-
这是
Object不是Array...正如@EliasVanOotegem 所说... -
请注意,您没有必须将结果集用作对象,您可以将结果作为关联数组的数组获取,也...添加了详细信息+我的答案的链接
标签: php mysql wordpress foreach