1

I'm trying to push values in a 2D array through foreach loop, I get some strange results while doing so. Please take a look at the code:

   foreach( $terms as $term ) {
                if(is_parent_tax($term)==TRUE){
                 $parent_term = get_term( $term->term_id, $taxonomy_name );
                    if(!in_array($parent_term->name,$term_list)){
                    $term_list[]['name']  = $parent_term->name ;
                    $term_list[]['slug'] = $parent_term->slug;
                    }
                }
              } 

Through a print_r test, I get the following results:

Array ( [0] => Array ( [name] => Category 2 ) [1] => Array ( [slug] => category-2 ) [2] => Array ( [name] => Category 3 ) [3] => Array ( [slug] => category-3 ) [4] => Array ( [name] => Category 4 ) [5] => Array ( [slug] => category-4 ) [6] => Array ( [name] => Category 2 ) [7] => Array ( [slug] => category-2 ) [8] => Array ( [name] => Category 1 ) [9] => Array ( [slug] => category-1 ) )

What I want instead, is:

Array ( [0] => Array ( [name] => Category 2 ) [0] => Array ( [slug] => category-2 ) [1] => Array ( [name] => Category 3 ) [1] => Array ( [slug] => category-3 ) [2] => Array ( [name] => Category 4 ) [2] => Array ( [slug] => category-4 ) [3] => Array ( [name] => Category 2 ) [3] => Array ( [slug] => category-2 ) [4] => Array ( [name] => Category 1 ) [4] => Array ( [slug] => category-1 ) )
0

3 Answers 3

2

Since no one explained why it didn't work.

Your usage of [] is incorrect. You're causing it to create another index. See Barth's answer although I would be using:

$term_list[] = array('name' => $parent_term->name, 'slug' => $parent_term->slug);

:)

Sign up to request clarification or add additional context in comments.

4 Comments

Ahan!!! You are right, I tried Barth's code at the very first, and that gave me the results as [name]=>[slug]=>value, so wrapping all this stuff in an array(); did the trick. Thanks all of you for your support and effort!
Glad to have helped :)
Definitely works! Only thing just to be clear for others looking at this, the (!in_array ...) part of the original question will not work as this is a multidimensional array. It's not really part of main question, but it tripped me up at first so just adding this for extra detail for others.
@Trevor php.net/manual/en/function.array-search.php have a look at the first user comment :)
2

Just a small change to your foreach can do the trick

foreach( $terms as $key => $term ) {
    if(is_parent_tax($term)==TRUE){
     $parent_term = get_term( $term->term_id, $taxonomy_name );
        if(!in_array($parent_term->name,$term_list)){
            $term_list[$key]['name']  = $parent_term->name ;
            $term_list[$key]['slug'] = $parent_term->slug;
        }
    }
} 

your

Array ( [0] => Array ( [name] => Category 2 ) [0] => Array ( [slug] => category-2 ) [1] => Array ( [name] => Category 3 ) [1] => Array ( [slug] => category-3 ) [2] => Array ( [name] => Category 4 ) [2] => Array ( [slug] => category-4 ) [3] => Array ( [name] => Category 2 ) [3] => Array ( [slug] => category-2 ) [4] => Array ( [name] => Category 1 ) [4] => Array ( [slug] => category-1 ) )

consists of duplicate keys which will be over written by other

The better way is to get it as

 Array
(
    [0] => Array
        (
            [name] =>  Category 2
            [slug] => category-2
        )

    [1] => Array
        (
            [name] =>  Category 3
            [slug] => category-3
        )
)

2 Comments

The problem is if they're not in the array they won't have a continuous progressive mapping of indices. But otherwise good answer +1
It's also better to promote "shorthand" PHP where applicable. Consolidation, organisation and efficiency is priority to going far in this industry.
1
$term_list[] = ['name' => $parent_term->Name, 'slug' => $parent_term->slug];

is the right way to go.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.