PHP数组以模为单位的第二个值 [英] Php array modulo every second value

查看:99
本文介绍了PHP数组以模为单位的第二个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.我有这个字母数组. 而且每个偶数字母都必须是!$字母,因此需要回显!b,!d,!f

I have a question. I have this array of the alphabet. And every even letter needs to be !$letter, so it needs to echo out !b, !d, !f

我不确定该怎么做.有人告诉我为此使用模数%.

I'm unsure how to do this. I've been told to use the modulo, % for this.

但是在Internet上阅读了很多有关它的信息后,对我来说还不清楚.

But reading a lot about it on the internet and things haven't gotten any clearer for me.

我感谢任何可以在此问题上为我提供帮助的人! 预先感谢!

I appreciate anyone that can help me on this matter! Thanks in advance!

推荐答案

foreach ($alphabet as $i => $letter) {
    echo (($i % 2) == 1 ? '!' : '') . $letter;
}

$i是字母在数组中的位置;由于数组索引从0开始,所以偶数字母将具有奇数索引.

$i is the position of the letter in the array; since array indexes start from 0, even letters will have odd indexes.

$i % 20,当其为奇数时,1.这是小学数学,应该是编程生涯的先决条件.

$i % 2 is 0 when $i is even, 1 when it's odd. This is grade school arithmetic, which should be a prerequisite for a programming career.

这篇关于PHP数组以模为单位的第二个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆