當我在WordPress中運行PHP
而不使用foreach
時,我成功地打印了一個多維數組。當我使用foreach
時,它返回一個error 500
我只是想循環遍歷結果,所以我能夠選擇每個name
,然後將它推送到另一個數組。多維數組Wordpress PHP MySQL查詢
如果有人能夠幫助我循環遍歷這個數組,那就太棒了!
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說'。 .. –
請注意,您不需要使用結果集作爲對象,並且您也可以將結果作爲關聯數組的數組獲得......添加了詳細信息+鏈接到我的答案 –