Slicing an array

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

How would I slice up this array to discard any values before $day and
one after, leaving 85 and 00 in this case?

$arr_days = (14,28,42,57,71,85,00);
$day = '57';

The $arr_days values are part of a school timetable I am building...14
is Monday because they are always the first two digits after the
decimal while dividing the first column in a seven day loop by seven
(assuming Mon period 1=1, Mon Period 2 = 8 etc.).

I am trying to identify the current day and grey out all the days
before and one after.

Many thanks,


Re: Slicing an array

Chris wrote:
Quoted text here. Click to load it

Lots of ways.

   Look up array_search to find the index of the needle and then
array_slice to build a new array after incrementing the index.

   You could also iterate through the array and push onto a new array
all values greater, then array_shift off the first element. That has the
"advantage" that the value would not have to be in the array.

PHP comes with an enormous collection of array functions.

   I'm assuming here that you wanted to learn to fish, not just get the


Quoted text here. Click to load it

Re: Slicing an array

Quoted text here. Click to load it

Thanks Jeff,

I haven't used array_search before, that should be the key (excusing
the pun).


Site Timeline