Home > OS >  how can I unique the value in foreach php?
how can I unique the value in foreach php?

Time:02-20

how can I unique the value here? its echo the same value more than one time!

foreach ( $products as $_product ) {

  foreach( wc_get_product_terms( $_product->get_id(), 'pa_color' ) as $attribute_value ){?>

    <input type="checkbox" /><?php echo esc_attr($attribute_value->name);

  }

}

here is the result: image of result

CodePudding user response:

$uniques = array ();

foreach ( $products as $_product ) {
  foreach( wc_get_product_terms( $_product->get_id(), 'pa_color' ) as $attribute_value ){
    if (in_array ($attribute_value->name), $uniques) {
       continue;
    }

    $uniques[] = $attribute_value->name;
?>
    <input type="checkbox" /><?php echo esc_attr($attribute_value->name);
  }
}

If you need to get unique names for each product ID, make a two-dimention array.

  • Related