简体   繁体   中英

Recursive infinite daily dynamic loop array php

I am trying to display a number every day in a loop. After the last element is reached it needs to get to the first element again. This needs to happen daily. I have overworked my brains out but did not managed to solve it. Function needs to return current number by day/hour/minute, like . This is what I tried till now.

<?php
function recursive_daily_deals($i = 1) {
    $current_date = strtotime(date('d-m-Y h:i:s'));
    $dbs_date_1 = strtotime('29-06-2017 8:20:16');
    $current_hour = date('h');
    var_dump($current_hour);
    $products = [
        451,
        455,
        453
    ];
    if ($i < count($products)) {
        return recursive_daily_deals($i+1);
    }
}
?>

EXPECTED output

>     First day - June 29 2017
>       It will appear 451
>     Second day - June 30 2017
>       It will appear 455
>     3rd day - July 1st 2017
>       It will appear 453
>     4th Day - July 2nd 2017
>        It will appear 453 
>     And start over

First you need to know how many days have been since the starting day. To do that you just need to sub the starting timestamp from the actual timestamp :

$dbs_date_1 = strtotime('29-06-2017 8:20:16');
$actualTimestamp = time();
$elapsedSec = $dbs_date_1 - $actualTimestamp;

// we need to get days from seconds
$elapsedDays = $elapsedSec / (3600*24);
$elapsedDays = floor($elapsedDays);

So when you have how many days have been since the starting day. We use floor() instead of round() because if the script runs after the half of the day it will return the number of days +1.

With this number of days we can have the number of cycles already done by dividing the number of elapsed days by the number of items in our array :

$nbItems = count($products);
$cycleCount = $elapsedDays / $nbItems;
$finishedCycles = floor($cycleCount);

We store the number of finished cycles by flooring the number of cycles. Then we just have to sub the days it took to complete those cycles from the elapsed days and we will get the position of the index.

 $completeDays = $finishedCycles * $nbItems;
 $actualPosition = $elapsedDays - $completeDays;
 return $products[$actualPosition];

While this is a simplified version of the code originally posted, I believe it contains the kind of logic that the OP seeks, as follows:

   <?php
   $products = [
        451,
        455,
        453
    ]; 

    $modfactor = count($products);
    $days = null;

    $weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];

    for ($i=0, $max = 7; $i < $max; $i++) {
          $days[$i] = $i % $modfactor;        
    }


    foreach ($weekdays as $dex => $wday) {
           echo "$wday, ";
           echo $products[ $days[$dex] ], "\n";
    }

See demo

Update: See demo here which makes use of array_map() and gets the current product ID, too.

While the loop is ever present, it is not infinite. If the number of products changes, then the modfactor changes, too. What stays constant are the days of the week. What makes the code work is taking advantage of a modulo operation.

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