[英]Echo every variable in an array
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. 因此
$value
是一个数组。 For every variable I'm removing the whitespace and replace it by a dash. 对于每个变量,我都删除空格,并用破折号代替。 I need to echo every variable outside the foreach function.
我需要在foreach函数之外回显每个变量。 I also tried to implode the variable first but with no results.
我也尝试先使变量内爆,但没有结果。 How to do this?
这个怎么做? Thanks!
谢谢!
Edit: The first var_dump($value);
编辑:第一个
var_dump($value);
gives me an array like: array(2) { [0]=> string(8) "Option 3" [1]=> string(8) "Option 4" }
给我一个像这样的数组:
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"
var_dump($ label)给出:
string(8) "option-3" string(8) "option-4"
I want to echo just this: option-3 option-4 我只想回应一下: option-3 option-4
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. 被放置在您的foreach循环之后。 So it's using the last values set for
$label
and $post_type
. 因此,它使用为
$label
和$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>";
}
You closed the foreach loop too early: 您太早关闭了foreach循环:
$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>"
}
Use the str_replace function, it takes a string and replaces all occurrences. 使用str_replace函数,它接受一个字符串并替换所有匹配项。
str_replace(' ','-',$label)
http://php.net/manual/en/function.str-replace.php http://php.net/manual/en/function.str-replace.php
You could use print_r()
. 您可以使用
print_r()
。 That will print the entire array with the keys and values. 这将使用键和值打印整个数组。
So after the foreach
, you would write: 因此,在
foreach
,您将编写:
print_r($value);
http://php.net/manual/en/function.print-r.php http://php.net/manual/en/function.print-r.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.