Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to loop over an array and when we reach the end start over?
    text
    copied!<p>I have an array of urls that I want to do pass to a function, I'll be using a cron job to pass only 2 of them every 10 minutes, I'm storing the last passed index of this array in a database, the problem is I don't know how to pass the first 2 elements when the last passed element is the last one in the array, let me explain with the code:</p> <pre><code>$sites = array( 'http://www.example.com/', 'http://www.example1.com/', 'http://www.example2.com/', 'http://www.example3.com/', 'http://www.example4.com/', 'http://www.example5.com/' ); // the number of urls to pass to the function // Edit: I forgot to say that this number might change later $sites_to_pass = 2; // this value is supposed to be stored when we finish processing the urls $last_passed_index = 2; // this is the next element's index to slice from $start_from = $last_passed_index + 1; // I also want to preserve the keys to keep track of the last passed index $to_pass = array_slice($sites, $start_from, $sites_to_pass, true); </code></pre> <p><code>array_slice()</code> is working fine, but when the <code>$last_passed_index</code> is <code>4</code> I only get the last element in the array, and when it's <code>5</code> (the last index) I get an empty array.</p> <p>What I want to do is when it's <code>4</code> to get the last element and the first element, and when it's <code>5</code> which is the last element's index to get the first 2 elements in the array.</p> <p>I'm not so good with php, any suggestions what should I do instead of creating a function to check the indexes ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload