配列内のすべての変数から空白を削除し、これを「-」に置き換えようとしています。ただし、配列の最後の変数のみを取得します。
私のコード:
<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