3

I am trying to filter a php array by comparing a field containing a unix date string ("start-unix-date") with the current unix time (simplified in this example as "15"). I want to keep only items where the unix date is in the future (>= than present).

Here is a simplified version of my code - based on this filtering method:

$my_array = array(
    array( 'start-date-unix' => '20' ), 
    array( 'start-date-unix'  => '10')
);

$unix_now = 15;

function filter_future($var) {
    return (is_array($var) && $var['start-date-unix'] >= $unix_now)
}

$filtered_array = array_filter($my_array, "filter_future");

var_dump($filtered_array);

The expected result:

The $filtered_array should keep only the item with a "start-date-unix" value of 20.

Actual result:

That filter doesn't do anything, every value is accepted. The $filtered_array still contains both items.

What works:

In the filter_future, if I enter the number (15) instead of the $unix_now variable, it works as expected: only the "20" item is kept.

So I suspect that there's an issue with $unix_now being a string vs an integer. I tried many methods of enforcing it being an integer but nothing seems to work :(

5 Answers 5

1

$unix_now is out of scope within the callback function:

Use a closure with the use keyword to pass the $unix_now value to the callback

$unix_now = 15;
$filtered_array = array_filter(
    $my_array, 
    function ($var) use ($unix_now) {
        return (is_array($var) && $var['start-date-unix'] >= $unix_now)
    }
);
Sign up to request clarification or add additional context in comments.

1 Comment

This seems like the more elegant option out of the 4 (so far) answers (IMHO)
0

$unix_now should be a global variable inside filter_future-function.

$unix_now = 15;

function filter_future($var) {
    global $unix_now;
    return (is_array($var) && (int) $var['start-date-unix'] >= $unix_now)
}

$filtered_array = array_filter($my_array, "filter_future");

var_dump($filtered_array);

The result:

array(1) { [0]=> array(1) { ["start-date-unix"]=> string(2) "20" } } 

1 Comment

Sure, it's just that stackoverflow imposes a 10 min delay before I could approve, and by then I was back in my workflow :)
0
$my_array = array(
    array( 'start-date-unix' => '20' ), 
    array( 'start-date-unix'  => '10')
);

$unix_now = 15;

foreach ($my_array as $i => $row)
{
    if ($row['start-date-unix'] < $unix_now)
    {
        unset($my_array[$i]);
    }
}

Comments

0

Change your function like below:

function filter_future($var) {
    $unix_now = 15;
    return (is_array($var) && $var['start-date-unix'] >= $unix_now);
}

Comments

0

move $unix_now = 15; inside function filter_future

function filter_future($var) {
    $unix_now = 15;
    return (is_array($var) && $var['start-date-unix'] >= $unix_now);
}

$unix_now inside your function is not recognize outside variable $unix_now. So put it inside your function is better

1 Comment

Actually that function, $unix_now, is being defined at a totally different place in my code. My example here was a very simplified version.

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.