简体   繁体   中英

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 .

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.

For clarity, the result of my $str given the above array would look something like this:

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. They're in groups of seven, so I need every 2nd, 3rd and 5th element, then skip 2 and start the process over. I'm going to use array_chunk , as suggested. 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

$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.

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. 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:

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)

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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