PHP访问数组
我有点困惑 我有一个数组:PHP访问数组,php,arrays,wordpress,Php,Arrays,Wordpress,我有点困惑 我有一个数组: <?php $terms = get_the_terms($post->ID, 'pf'); print_r($terms); ?> 有什么想法吗 更新 我不能使用$term[15]->slug,因为我的脚本将基于[taxonomy](本例中为pf)!:)因此,如果没有foreach循环,就不可能做到这一点。术语数组15索引包含这样的对象访问 echo $term[15]->slug 在内部arry的索引15处有一个st
<?php
$terms = get_the_terms($post->ID, 'pf');
print_r($terms);
?>
有什么想法吗
更新
我不能使用$term[15]->slug,因为我的脚本将基于[taxonomy](本例中为pf)!:)因此,如果没有foreach循环,就不可能做到这一点。术语数组15索引包含这样的对象访问
echo $term[15]->slug
在内部arry的索引15处有一个stdclass对象,可以通过强制转换将其转换/访问为数组,但请尝试此方法
$term[15]->slug
根据Pekka的回答,如果重新格式化print_r()输出,您将得到:
Array (
[15] => stdClass Object (
[term_id] => 15
[name] => Text
[slug] => text
[term_group] => 0
[term_taxonomy_id] => 33
[taxonomy] => pf
[description] => An PF article.
[parent] => 0
[count] => 3
[object_id] => 694
)
)
使用print_r()转储变量时,最好使用
标记包围调用-print_r不会对数据进行任何HTML化,因此在HTML页面中查看时,它对数组所做的良好缩进会丢失。使用
标记保留格式。使用var_dump()
也会做同样的事情,但也会将类型/大小数据添加到转储输出。我没有投反对票,但这并没有回答OP的问题在我写这篇文章时,pekka的答案仍然在列表中。但是,他现在删除了它。有没有一种方法可以通过“分类法”(在本例中是pf)访问$terms?因为ID可能会有所不同。@meritum:ID会有所不同。您必须在此处使用foreach
循环foreach($term as$object){echo$object->slug;}
$term[15]->slug
Array (
[15] => stdClass Object (
[term_id] => 15
[name] => Text
[slug] => text
[term_group] => 0
[term_taxonomy_id] => 33
[taxonomy] => pf
[description] => An PF article.
[parent] => 0
[count] => 3
[object_id] => 694
)
)