0

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

3
  • Why can't you just put the <li> tag in the foreach? (wrap it with end-php and start-php tags). Commented Sep 23, 2013 at 21:40
  • One thing I notice in your new edit with the while is you're using the alternate syntax (while ():) instead of using the standard syntax and I don't see and endwhile; 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. Commented Sep 23, 2013 at 22:30
  • In addition to the above, since this isn't a complete snippet, can you make a small snippet with that is executable that we can test and describe better what kind of outcome you'd like? As it stands I cannot run your code and see your results therefor it makes it that much harder for me to try and pinpoint where your error may lie. Commented Sep 23, 2013 at 22:33

4 Answers 4

3

You are only getting the last one because your echo line:

<li class="item <?php echo $post_type ?> <?php echo $label ?>"></li>

Is placed after your foreach loop. So it's using the last values set for $label and $post_type. Try placing that inside your loop so the echo is generated every time you loop over the list.

You should end up with something like the following:

$value = get_field($key);

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);


    echo "<li class=\"item $post_type $label\"></li>";
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. This might work indeed. But what if you want to put this outside the loop? I that possible?
If you're printing an li for each item in the array you never want to put it outside of the loop. Otherwise you will never end up with all the values echoed. There will always need to be some kind of loop to display an unknown number of items.
Thanks for your help. I'm still not getting what I want. Maybe it's because the list is generated by a while function (see the edit in my question).
In further addendum to what I've posted above on your original question (as comments), now that I more of an understanding of what you're doing. You would still need to output the <li> in ever iteration of the foreach as you're using a variable generated in the foreach in your output. If you only want one (1) <li> per while iteration then your current format is doing exactly as you intended
1

You closed the foreach loop too early:

$value = get_field($key);

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);


echo "<li class='item $post_type $label'></li>"
}

7 Comments

Tell me if I'm wrong but I don't think variables are interpolated in single quoted strings. I'm not a PHP expert but I know several languages use follow that same construct.
I usually don't really bother about using either ' or " (in case i copied old html wich used "), and it never broke with me. But i might be wrong
It doesn't appear to work, here's the test at phpFiddle.org. Something to keep in mind in the future.
Thanks izuriel, i always use ' in html when writing myself and if someone else wrote it, i'm just to lazy to rewrite or \".
In further addendum to what I've posted above on your original question (as comments), now that I more of an understanding of what you're doing. You would still need to output the <li> in ever iteration of the foreach as you're using a variable generated in the foreach in your output. If you only want one (1) <li> per while iteration then your current format is doing exactly as you intended.
|
0

Use the str_replace function, it takes a string and replaces all occurrences.

str_replace(' ','-',$label)

http://php.net/manual/en/function.str-replace.php

Comments

0

You could use print_r(). That will print the entire array with the keys and values.

So after the foreach, you would write:

print_r($value);

http://php.net/manual/en/function.print-r.php

Comments

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.