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 ) )