I'm trying to remove whitespaces and replace this with a '-' from every variale in an array. However I only get the last variable of the array.
My code:
<ul class="gap-items">
<?php
while ($query->have_posts()):
$query->the_post();
$post_type = get_post_type( get_the_ID() );
$key = 'field_5208f1f811702';
$value = get_field($key);
var_dump($value);
foreach ($value as $label) {
$label = strtolower($label);
$label = preg_replace("/[^a-z0-9_\s-]/", "", $label);
//Clean up multiple dashes or whitespaces
$label = preg_replace("/[\s-]+/", " ", $label);
//Convert whitespaces and underscore to dash
$label = preg_replace("/[\s_]/", "-", $label);
var_dump($label);
}
?>
<!-- Loop posts -->
<li class="item <?php echo $post_type ?> <?php echo $label ?>" id="<?php the_ID(); ?>" data-permalink="<?php the_permalink(); ?>">
So$value is an array. For every variable I'm removing the whitespace and replace it by a dash. I need to echo every variable outside the foreach function. I also tried to implode the variable first but with no results. How to do this? Thanks!
Edit: The first var_dump($value); gives me an array like:
array(2) {
[0]=>
string(8) "Option 3"
[1]=>
string(8) "Option 4"
}
the var_dump($label) gives:
string(8) "option-3"
string(8) "option-4"
I want to echo just this: option-3 option-4
while ():) instead of using the standard syntax and I don't see andendwhile;in your code sample which makes it incomplete. But you're looping in the while with a foreach inside of the while, is that a correct assumption? Also, try and format your code neatly when posting here, that can help you find issues and it aids in our ability to follow along with what you're doing.