简体   繁体   English

PHP foreach获取迭代索引

[英]PHP foreach get iteration index

I'm trying to display results in a foreach loop. 我正在尝试在foreach循环中显示结果。

I have an array like so: 我有一个像这样的数组:

$slides = array(
    1 => $params->get('param_1'), 
    2 => $params->get('param_2'),
    3 => $params->get('param_3')
);

If a parameter in the backend is set to yes , the value equals 1 , and thus a result is displayed. 如果后端中的参数设置为yes ,则该值等于1 ,因此将显示结果。

So what I'm trying to write is, foreach array value that is equal to 1 所以我想写的是, foreach数组值等于1

I'm aware that I can't use something like this: 我知道我不能使用这样的东西:

foreach (slides == 1) { 
    // echo stuff here
}

How would I write this foreach loop? 我将如何编写此foreach循环? Any help or guidelines would be highly appreciated 任何帮助或指导方针将不胜感激

foreach ( $slides as $slide ) { 
    if ( $slide != 1 ) continue;
    // echo stuff here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM