
[英]trying to print 2 arrays in 2 columns on pdf but the first array prints correct while the second array prints clustered, values on top of each other
[英]How to print all values from array, It prints only first?
我遇到了问题,但无法解决。
// get list of categories
$terms = $listing->get_field( 'region' );
foreach($terms as $t){
$parent_category = mg_get_term_parents( $t->term_id, 'region' );
}
当我转储 parent_category 时,我得到一个数组(它只打印祖父母和父母),用于子代码位于底部。
array(2) {
[0]=>
object(WP_Term)#6446 (10) {
["term_id"]=>
int(213)
["name"]=>
string(8) "Americas"
["slug"]=>
string(8) "americas"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(213)
["taxonomy"]=>
string(6) "region"
}
[1]=>
object(WP_Term)#6448 (10) {
["term_id"]=>
int(157)
["name"]=>
string(13) "United States"
["slug"]=>
string(13) "united-states"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(157)
["taxonomy"]=>
string(6) "region"
}
}
或者如果没有出现祖父母,我会得到一个数组。
object(WP_Term)#6454 (10) {
["term_id"]=>
int(214)
["name"]=>
string(4) "EMEA"
["slug"]=>
string(4) "emea"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(214)
["taxonomy"]=>
string(6) "region"
}
如果我只有父母和孩子,我使用以下方法,它给了我很好的结果。 但不确定需要在此处添加什么以涵盖所有情况。
foreach( $terms as $t )
{
if ( $parent_category = mg_get_term_parents( $t->term_id, 'region' ) )
{
array_unshift( $terms, $parent_category );
}
}
$formatted_terms = array_filter( array_map( function( $term ) {
if ( ! $term = \MyListing\Src\Term::get( $term ) ) {
return false;
}
return [
'link' => $term->get_link(),
'name' => $term->get_name(),
'color' => $term->get_color(),
'text_color' => $term->get_text_color(),
'icon' => $term->get_icon( [ 'background' => false, 'color' => false] ),
];
}, $terms ) );
当我有祖父母,父母和孩子时会出现问题。 上面的解决方案不起作用。
如果我使用
$formatted_terms = array_filter( array_map( function( $term ) {
if ( ! $term = \MyListing\Src\Term::get( $term ) ) {
return false;
}
return [
'link' => $term->get_link(),
'name' => $term->get_name(),
'color' => $term->get_color(),
'text_color' => $term->get_text_color(),
'icon' => $term->get_icon( [ 'background' => false, 'color' => false] ),
];
}, $terms ) );
我只打印数组中的 1 个区域,而缺少的区域是一个孩子。 数组只输出父母和祖父母。 我需要以某种方式将数组输出添加到上面的代码中以得到所有需要的东西。
知道如何做到这一点吗?
为了处理嵌套在数组中嵌套的数组中的数组(无限……),您需要使用循环处理每个数组,或者构建一个递归函数来为您执行此操作。
我目前是一个严重依赖自定义分类法和术语结构的网站的首席工程师。 这是我用来构建一个完全分层的分类术语数组的简单函数:
function hierarchical_term_list( Array &$terms, Array &$into, $parent_id = 0 ){
foreach( $terms as $i => $term ){
if( $term->parent == $parent_id ){
$into[] = $term;
unset($terms[$i]);
}
}
foreach( $into as $top_term ){
$top_term->children = array();
hierarchical_term_list( $terms, $top_term->children, $top_term->term_id );
}
}
现在,一旦我得到我的术语列表:
$terms = get_terms(array(
'hide_empty' => true,
'taxonomy' => 'some_tax',
'post_type' => 'some_post_type'
));
$hierarchy = array();
hierarchical_term_list( $terms, $hierarchy );
这给了我一个整洁的WP_Term
对象数组,像这样(为简洁起见被截断)
array(2) {
[0] => object(WP_Term)#1343 (12) {
["term_id"] => int(5)
["name"] => string(4) "Test"
…
["children"] => array(1) {
[0] => object(WP_Term)#1344 (12) {
["term_id"] => int(16)
["name"] => string(10) "Test Child"
…
["children"] => array(2) {
[0] => object(WP_Term)#1345 (12) {
["term_id"] => int(17)
["name"] => string(16) "Test Grand Child"
["slug"] => string(16) "test-grand-child"
…
["children"] => array(0) {}
}
[1] => object(WP_Term)#1346 (12) {
["term_id"] => int(18)
["name"] => string(20) "Favorite Grand Child"
["slug"] => string(20) "favorite-grand-child"
…
["children"] => array(0) {}
}
}
}
}
}
[1] => object(WP_Term)#1347 (12) {
["term_id"] =>int(1)
["name"] => string(13) "Childless"
["slug"] => string(13) "childless"
…
["children"] => array(0) {}
}
}
现在到了重要的部分——你如何在数组中的数组中输出这些数组......这就是我上面提到的递归函数发挥作用的地方。
首先,我们然后需要将我们的分层数组作为参数传递给函数,然后如果其中有children
,则再次调用自身,而不是使用子级数组作为参数。 这将一直持续下去,直到没有更多的孩子,此时它将在递归深度中“向上返回”,直到到达下一项,在它尚未处理的最接近的级别。
这是我使用的函数的简化版本:
function output_hierarchical_term_list( $terms ){
echo '<ul class="hierarchical-terms">';
foreach( $terms as $term ){
printf( '<li>%s', $term->name );
if( !empty( $term->children ) ){
output_hierarchical_term_list( $term->children );
}
echo '</li>';
}
echo '</ul>';
}
这将输出类似于以下内容的内容:
<ul class="hierarchical-terms">
<li>
<strong>Test</strong>
<ul class="hierarchical-terms">
<li>
<strong>Test Child</strong>
<ul class="hierarchical-terms">
<li><strong>Test Grand Child</strong></li>
<li><strong>Favorite Grand Child</strong></li>
</ul>
</li>
</ul>
</li>
<li>
<strong>Childless</strong>
</li>
</ul>
希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.