2

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.

3
  • is it have the 0 index Commented Jan 16, 2017 at 14:36
  • I guess so, as it's the first. Unless I'm being thick, which is entirely possible. Commented Jan 16, 2017 at 14:40
  • Okay, so no, it doesn't have the 0 index. Will update the question. Commented Jan 16, 2017 at 14:44

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Was starting to think I was going to need to use while rather than foreach, thanks very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.