0

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
)
4
  • What is the desired outcome? Commented Jan 13, 2017 at 8:02
  • What i want is: Array ( [0] => Array ( [name] => Salades [slug] => salades [id] => 67 ) [1] => Array ( [name] => Aardappels [slug] => aardappels [id] => 23 ) [3] => Array ( [name] => Diverse fruit [slug] => diverse-fruit [id] => 11 ) etc. etc. Commented Jan 13, 2017 at 8:07
  • 2
    why are you calling break; this will break out of the loop? Commented Jan 13, 2017 at 8:13
  • 1
    Addition to @atoms $product_cat's value will be overwritten in every loop if removed break. should be $product_cat[] = value. Commented Jan 13, 2017 at 8:16

2 Answers 2

1

Try changing your foreach to work as followed:

foreach ($terms as $term) {

    $array2 = array (  
        'name' => $term->name,  
        'slug' => $term->slug,
        'id' => $term->term_id
    );





    $product_cat[] = $array2;
}

As requested, the output should be something like:

Array ( [0] => Array ( [name] => Salades [slug] => salades [id] => 67 ) [1] => Array ( [name] => Aardappels [slug] => aardappels [id] => 23 ) [3] => Array ( [name] => Diverse fruit [slug] => diverse-fruit [id] => 11 )

Break was causing you to exit the loop after one iteration. And as mentioned by Sougata you would be overwriting product_cat unless you append it with product_cat[]

UPDATE: Assuming that you want to sort arrays and 'id' is unique:

foreach ($terms as $term) {

    // check if its set, if not add it!
    if(!isset($product_cat[$term->term_id])){
        $product_cat[$term->term_id] = array('name'=>$term->name, 'slug' => $term->slug);
    }

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

6 Comments

Output result is: Array ( [0] => Array ( [0] => Array ( [name] => Salades [slug] => salades [id] => 67 ) ) )
is that still only the first iteration then? if so check to make sure your DB call is returning the data you expect
Your right the result is good, now i want to filter the duplicated arrays.. if i put array_unique($product_cat); than i get one array again..
you dont need to use array_unique for that. Just check if it exsists already before adding it. N.B. you shouldnt get the rows from DB unless you need them. This is probably a DB issue
' [0] => Array ( [name] => Salades [slug] => salades ) [1] => Array ( [name] => Aardappels [slug] => aardappels ) [2] => Array ( [name] => Salades [slug] => salades ) '
|
0

just remove the

break;

and put

array_push( $catarray, $product_cat );

into the foreach loop

$catarray = 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
        );

        array_push( $catarray, $array2 );
    }

endwhile;
$categorielijst = array_unique($catarray);

echo '<pre>';
print_r($categorielijst);
echo '</pre>';

1 Comment

Hi there, the solution was good. But it is a paged page so on page 1 you see not all terms and on page 2 you see diffrent. But i want that we get all the terms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.