[英]How to operate on every 2nd, 3rd and 5th element in an array?
I have a pretty simple indexed array: 我有一个非常简单的索引数组:
Array
(
[0] => 1
[1] => Buster Posey
[2] => SF
[3] => C
[4] => 16.60
[5] => 5
[6] => 28
[7] => 2
[8] => Joe Mauer
[9] => Min
[10] => C
[11] => 57.60
[12] => 35
[13] => 80
...
I need to add every 2nd, 3rd and 5th key value to $str
. 我需要将每个第二,第三和第五个键值添加到$str
。
I can handle operating on the $str
, but I can't wrap my tiny little brain around the loop(s)/counter(s) I need, so just a nudge in the right direction would be great. 我可以在$str
上进行操作,但是我无法将我的小脑袋缠绕在我需要的循环/计数器上,因此,向正确方向轻推将是不错的选择。
For clarity, the result of my $str
given the above array would look something like this: 为了清楚起见,给出上述数组的$str
结果看起来像这样:
Buster Posey, SF, 16.60
Joe Mauer, Min, 57.60
That's the 2nd, 3rd and 5th element of the array with a couple commas and a newline added in for good measure repeated all the way down the array. 这是数组的第二,第三和第五个元素,带有几个逗号,并添加了换行符,以很好的方式在数组中一直重复。
EDIT: 编辑:
I have an error with my question. 我的问题有一个错误。 The comment by salathe below is correct. salathe的评论在下面是正确的。 They're in groups of seven, so I need every 2nd, 3rd and 5th element, then skip 2 and start the process over. 它们以七个为一组,因此我需要每个第二,第三和第五个元素,然后跳过2并重新开始该过程。 I'm going to use array_chunk , as suggested. 如建议的那样 ,我将使用array_chunk 。 Thanks for all of the answers. 感谢您提供所有答案。
Something like this? 像这样吗
$i = 0;
foreach($array as $value) {
if ($i++ % 2 == 0 || $i++ % 3 == 0 || $i++ % 5 == 0) {
echo $value;
}
}
You need to use the modulo operator ( %
). 您需要使用模运算符( %
)。 It returns the remainder of the division of a number, and is routinely used to check if a number is a multiple of another. 它返回数字除法的余数,通常用于检查数字是否为另一个的倍数。
So, to check is a number is even ( exactly divisible by 2 ), you do 因此,要检查数字是否为偶数(正好可被2整除),您可以
$odd = $number%2;
To make it a complete answer, you use this criteria inside a loop (don't know how to do so without one) : 要给出完整的答案,请在循环中使用此条件(不知道如何使用此条件):
for($i=0; $i<count($array)-1;$i++){
if($i%5==0) { $str .= $array[$i]; }
}
Two different approaches: loop over all entries and use mod, or loop for each different condition. 两种不同的方法:循环所有条目并使用mod,或针对每个不同的条件循环。
All in one loop: 一圈循环:
foreach ($array as $k => $v){
if ($k % 2 == 0 || $k % 3 == 0 || $k % 5 == 0){
.. do something with $v
}
}
Different loops: 不同的循环:
for ($i=0; $i<count($array); $i+=2) .. do something with $array[$i]
for ($i=0; $i<count($array); $i+=3) .. do something with $array[$i]
for ($i=0; $i<count($array); $i+=5) .. do something with $array[$i]
You need to take sequences with steps 2-1-2, 2-1-2. 您需要按照步骤2-1-2、2-1-2进行操作。 You can do this with double loop,loop + case... 你可以用双循环,循环+案例...
I don't quite know PHP, but if I understand correctly, you need to concatenate the indexes 2, 3, 5 and perhaps more, following a progression: 我不太了解PHP,但是如果我理解正确的话,请按照以下步骤连接索引2、3、5甚至更多:
n[0] = 2
n[1] = 3
n[2] = 5
...
n[i] = n[i-1] + n[i-2]
So you might need to create an array of the indexes you need to concatenate, providing the first two values (looks like a Fibonacci series to me): 因此,您可能需要创建需要连接的索引的数组,并提供前两个值(在我看来像是斐波那契数列):
n[0] = 2;
n[1] = 3;
for(i=2, i<m, i++) {
n[i] = n[i-1] + n[i-2];
}
Then, you can use this array to concatenate the desired values from your array: 然后,您可以使用此数组从数组中连接所需的值:
for(i=0; i<m; i++) {
str = concatenate(str, arr[n[i]]);
}
(I know the syntax is not PHP... but I think it illustrates the idea) (我知道语法不是PHP ...但是我认为它说明了这个想法)
If you need a different progression, you need to answer this question: how do I calculate the next value, using the previous value(s)? 如果您需要不同的级数,则需要回答以下问题:如何使用以前的值来计算下一个值? Then you can build an algorithm that makes the work. 然后,您可以构建一个可以完成工作的算法。
Hope this helps you. 希望这对您有所帮助。
foreach( array_chunk( $array, 7) as $row) {
echo $row[1] . ' , ' . $row[2] . ' , ' . $row[4] . "\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.