There are quite a few issues with your code so we really need to start again. Based on what you've provided and without knowing exactly how your ACF repeater is set up, I think the following should work for you.
What it appears that you want to do is:
- Loop through all products in your repeater
- Get all values from your
product_attributes
- Get the unique values across all products
- Display the unique values on the same line, separated by spaces
The main problem you are having is getting the unique array. There are a number of ways to do this, you chose the most complicated :)
1. Use in_array to check previous values
This is the way you are trying to do it at the moment, but you are having problems with the logic. Therefore I'd suggest option 2, but for completeness this is how you should do it:
$filter_attributes = array();
if (have_rows('product')): while (have_rows('product')) : the_row();
$attributes = get_sub_field_object('product_attributes');
$values = $attributes['value'];
if($values) {
foreach ($values as $value)
if (!in_array($value, $filter_attributes)) {
$filter_attributes[] = $value;
}
}
} endwhile; endif;
/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes );
2. Use array_unique
The code for this is much simpler than the previous option. You save all values into an array and then at the end use array_unique
(PHP.net array_unique). This eliminates the need for checking the existing values every time. e.g.
$all_attributes = array();
$filter_attributes = array();
if (have_rows('product')): while (have_rows('product')) : the_row();
$attributes = get_sub_field_object('product_attributes');
$values = $attributes['value'];
if($values) {
foreach ($values as $value)
$all_attributes[] = $value; /* Save ALL values */
}
} endwhile; endif;
/* use array_unique to remove duplicates from the array */
$filter_attributes = array_unique ($all_attributes);
/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes );
3. Use the array key
If you use the value for the array key, then that will ensure each values will be unique because duplicate keys are not allowed in the array. It's slightly hack-y way, but its quick & easy :)
$filter_attributes = array();
if (have_rows('product')): while (have_rows('product')) : the_row();
$attributes = get_sub_field_object('product_attributes');
$values = $attributes['value'];
if($values) {
foreach ($values as $value)
/* if $all_attributes[$value] already exists, this will overwrite it
ensuring the array only has unique values */
$all_attributes[$value] = $value;
}
} endwhile; endif;
/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes );
$values
coming from? I presume that should be$attributes
? – FluffyKitten$filter_attributes
array has duplicates? How is it working at all then because you are still using$values
in yourforeach
loop? What is$values
and how does it have the right values? I'm not sure what your code is trying to do. What are you hoping to achieve byif (in_array...) continue
? – FluffyKitten$values
is the ACF checkbox value. I'm trying to output a single instance of each attribute – SBWTif (in_array...) continue
? That has no effect whatsoever. – FluffyKitten