Home>

In the title of the blog, "No. 0…" should be read as "Obtain the No. 0 part,
From the 1st to the 10th, from the 11th to the 20th, from the 21st to the ...

▽ 1st to 10th
・ 10th
・ 9th
・ 8th
~
・ 1st

Error message

Minimum and maximum values ​​are obtained with min and max to obtain the location of "▽ 1st to 10th".
Also, because I wanted to get 10 pieces each, when the remainder becomes 1, $i is incremented and I am trying to put together 10 cases.
Not added in order,
For example, if you register the 23rd blog instead of the 21st after the 20th, $i will not be incremented and will be added to the same array.

Applicable source code
$i = 0;
foreach ($times_data as $key =>$value) {
    $array_times [$i] ['times'] [$key] = $value;
    if ($key% 10 === 1) {
        //minimum value
        $array_times [$i] ['min'] = min (array_keys ($array_times [$i] ['times']));
        //Maximum value
        $array_times [$i] ['max'] = max (array_keys ($array_times [$i] ['times']));
        $i ++;
    }
}
The above $times_data is the following associative array.
Array
(
    [20] =>Array
        (
            [id] =>19
            [times] =>20
        )
    [19] =>Array
        (
            [id] =>20
            [times] =>19
        )
    [17] =>Array
        (
            [id] =>17
            [times] =>17
        )
    [16] =>Array
        (
            [id] =>16
            [times] =>16
        )
    [15] =>Array
        (
            [id] =>15
            [times] =>15
        )
    [14] =>Array
        (
            [id] =>14
            [times] =>14
        )
    [13] =>Array
        (
            [id] =>13
            [times] =>13
        )
    [12] =>Array
        (
            [id] =>12
            [times] =>12
        )
    [11] =>Array
        (
            [id] =>11
            [times] =>11
        )
    [10] =>Array
        (
            [id] =>10
            [times] =>10
        )
    [9] =>Array
        (
            [id] =>9
            [times] =>9
        )
    [8] =>Array
        (
            [id] =>8
            [times] =>8
        )
    [7] =>Array
        (
            [id] =>7
            [times] =>7
        )
    [6] =>Array
        (
            [id] =>6
            [times] =>6
        )
    [5] =>Array
        (
            [id] =>5
            [times] =>5
        )
    [4] =>Array
        (
            [id] =>4
            [times] =>4
        )
    [3] =>Array
        (
            [id] =>3
            [times] =>3
        )
    [2] =>Array
        (
            [id] =>2
            [times] =>2
        )
    [1] =>Array
        (
            [id] =>1
            [times] =>1
        )
)

I would like to display 10 items at a time, from 1st to 10th, 11th to 20th, 21st to .... How should I do it?

  • Answer # 1

    If you look at the question,
    I thought that I wanted to summarize "ten times (even without 10)" rather than "every ten". . .

    ■ As a prerequisite,

    Get blog data in ascending order of times. (If you want to line up later, in descending order)

    ■ The sample code looks like this.

    $array_times = array ();
    $last_time_idx = 0;
    $i = 0;
    foreach ($times_data as $value) {
        $now_time = $value ['times'];
        // ★ "quotient" rounded up divided by 10
        $time_idx = ceil ($value ['times']/10);
        // Compare with previous time_idx (* However, $last_time_idx = 0 is not processed because there is no previous data)
        if ($last_time_idx>0&&$last_time_idx! = $time_idx) {
            $i ++;
        }
        // Write the initialization process properly
        if (! isset ($array_times [$i])) {
            $array_times [$i] = array ('times' =>array ());
        }
        // To get the minimum and maximum values ​​correctly, set the key to the value of times
        $array_times [$i] ['times'] [$now_time] = $value;
        //minimum value
        $array_times [$i] ['min'] = min (array_keys ($array_times [$i] ['times']));
        //Maximum value
        $array_times [$i] ['max'] = max (array_keys ($array_times [$i] ['times']));
            // The current $time_idx becomes the next "$time_idx"
        $last_time_idx = $time_idx;
    }

    The points below are the points.

    // Round up the quotient divided by 10
        $time_idx = ceil ($value ['times']/10);

    1-10 divided by 10 and rounded up to all 1's.
    Divide 11-20 by 10 and round up to "2".
    Divide 21-30 by 10 and round up to all three ...

    and 10 times you want to summarize, the same answer will always be returned, so use this for the judgment.

    By incrementing$ionly when the previous data and$time_idxof the current data are different, collect 10 times each You can.
    Even if the number is skipped, it will not be affected.


    If you really just want to put together "10 cases", you don't need to be so difficult.

    $array_times = array ();
    $i = 0;
    foreach ($times_data as $value) {
        // 10 When you have collected all, just increment it ...
        if (isset ($array_times [$i])&&count ($array_times [$i] ['times']) == 10) {
            $i ++;
        }
        // Write the initialization process properly
        if (! isset ($array_times [$i])) {
            $array_times [$i] = array ('times' =>array ());
        }
        $_now_time = $value ['times'];
        // To get the minimum and maximum values ​​correctly, set the key to the value of times
        $array_times [$i] ['times'] [$_ now_time] = $value;
        //minimum value
        $array_times [$i] ['min'] = min (array_keys ($array_times [$i] ['times']));
        //Maximum value
        $array_times [$i] ['max'] = max (array_keys ($array_times [$i] ['times']));
    }