I am busy with a foreach loop but i can't find the problem. This code show only first array, and stops. The problem comes from the array in the foreach loop.
Here is my code:
<?php
$catarray = array();
$catslug = array();
while ( have_posts() ) : the_post();
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$array2 = array (
'name' => $term->name,
'slug' => $term->slug,
'id' => $term->term_id
);
$product_cat = $array2;
//$product_cat = $term->name;
break;
}
array_push( $catarray, $product_cat );
endwhile;
$categorielijst = array_unique($catarray);
echo '<pre>';
print_r($categorielijst);
echo '</pre>';
?>
The result is:
Array
(
[0] => Array
(
[name] => Salades
[slug] => salades
[id] => 67
)
)
And if i change $product_cat = $array2; for $product_cat = $term->name;
Than is the output:
Array
(
[0] => Salades
[1] => Aardappels
[3] => Diverse fruit
[4] => Blad groentes
[5] => Schulp sappen 100% fruit en groentes
[8] => Groentes
[11] => Uien
[13] => Verse kruiden
[19] => Dressings kiooms
[25] => Appels
[28] => Paddenstoelen
[32] => Tomaten
[34] => Bananen
[35] => Citrus fruit
[37] => Peren
[49] => Verse sla
)