0

配列内のすべての変数から空白を削除し、これを「-」に置き換えようとしています。ただし、配列の最後の変数のみを取得します。

私のコード:

<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(); ?>">

配列$valueも同様です。すべての変数について、空白を削除してダッシュに置き換えます。foreach 関数の外側にあるすべての変数をエコーする必要があります。また、最初に変数を内破しようとしましたが、結果はありませんでした。これを行う方法?ありがとう!

編集:最初var_dump($value);の配列は次のようになります。 array(2) { [0]=> string(8) "Option 3" [1]=> string(8) "Option 4" }

var_dump($label) は以下を提供します: string(8) "option-3" string(8) "option-4"

これだけをエコーし​​たい: option-3 option-4

4

4 に答える 4

0

str_replace 関数を使用します。文字列を取り、すべての出現箇所を置き換えます。

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

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

于 2013-09-23T21:42:28.457 に答える
0

を使用できますprint_r()。これにより、キーと値を含む配列全体が出力されます。

の後にforeach、次のように記述します。

print_r($value);

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

于 2013-09-23T22:27:36.857 に答える