Badger
Badger

Reputation: 487

Get value of next element in array of arrays

I have an array containing an array key of a date, with the value being another array.

Array ( [2017-01-01] => Array ( [available] => 1 ) [2017-01-02] => Array ( [available] => ) [2017-01-03] => Array ( [available] => 1 ) )

I'm trying to find a way of seeing if an unavailable day is the last or first of a given set of unavailable days. I'm using the following code, but getting an error of "Only variables can be passed by reference".

foreach ( $calendar as $date ) {
    if ( $date["available"] == 1 ) {
        if ( next( $date["available"] != 1 ) ) {
            echo $date . ' end';
        }
    }
}

I can see why this isn't working, but can't think of a way I can achieve what I want.

Upvotes: 2

Views: 583

Answers (1)

LF-DevJourney
LF-DevJourney

Reputation: 28529

you cannot use next on an value, next is used on array inner index.

<?php
$calendar = array ( '2017-01-01' => array ( 'available' => 1 ), '2017-01-02' => array ( 'available' => ''), '2017-01-03' => array ( 'available' => 1 ) );
while(current($calendar))
{
    if(current($calendar)['available'] == 1)
    {
        if(next($calendar)['available'] != 1)
            var_dump(current($calendar));
            echo 'end'."\n";
    }
    next($calendar);
}

Upvotes: 2

Related Questions