【问题标题】:How to print all values from array, It prints only first?如何打印数组中的所有值,它只打印第一个?
【发布时间】:2020-01-28 22:29:56
【问题描述】:

我遇到了问题,无法解决。

// 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 个区域,而缺少的区域是一个孩子。数组仅输出父母和祖父母。我需要以某种方式将数组输出添加到上面的代码中 tog et all required.

知道如何做到这一点吗?

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    为了处理嵌套在数组中的数组(无穷无尽……),您需要使用循环处理每个数组,或者构建一个 Recursive Function 来为您完成。

    我目前是一个严重依赖自定义分类法和术语结构的网站的首席工程师。下面是一个简单的函数,我用它来构建一个完全分层的分类术语数组:

    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>
    

    希望对您有所帮助!

    【讨论】:

    • 谢谢。我是编程新手,不太懂
    猜你喜欢
    • 2021-11-29
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2019-01-19
    相关资源
    最近更新 更多