0

ell, my first thought was to use array_filter function looks something like this:

function gettime(){


$cityciqurechangetime = an array of integers;
$iniFirstLegEndTime = an integer calculated by other methods;

$CitySquareTimearray = array_filter($cityciqurechangetime, 
                                   function($n){global $iniFirstLegEndTime ;
                                   return $n >= ($iniFirstLegEndTime);});

return $CitySquareTimearray;

}

But it's not working, i ran a few tests and the results indicated that the variable $iniFirstLegEndTime has never passed to the callback function in the array_filter() function so the $CitySquareTimearray variable is just the entire array $cityciqurechangetime.

I once thought maybe i shouldn't declare a function in the array_filter() function but the following one works perfectly where $starttimetabletime is an array of time stamps.

$initStartTimearray = array_filter($starttimetabletime, 
                                    function($n) 
                                    {return $n >= time();});

What i really want is an "sub-array" of $cityciqurechangetime with all element is greater or equal than $iniFirstLegEndTime, please tell me what i did wrong or if there are better ways to solve this problem other than using array_filter() function please teach me, thank you very much.

1
  • You use global inside of anon function, but do not globalize in gettime(). It will not work. Commented Apr 24, 2016 at 23:09

2 Answers 2

2

Your return statement is incorrect. Here is an updated version of your script:

function gettime(){


    $cityciqurechangetime = [1,2,3,4,5];
    $iniFirstLegEndTime = 2;

    $CitySquareTimearray = array_filter($cityciqurechangetime, function($n) use ($iniFirstLegEndTime){
        return $n >= ($iniFirstLegEndTime)?$n:null;
    });

    return $CitySquareTimearray;

}

var_dump(gettime());

You should be using array filter, as it is much more performant then iterations over arrays.

Sign up to request clarification or add additional context in comments.

Comments

0

The updated function provided by @null05 above is the correct way to avoid the use of the global keyword and the return statement can be further simplified without the use of ternary operators.

function gettime() {
    $cityciqurechangetime = [1,2,3,4,5];
    $iniFirstLegEndTime = 2;

    $CitySquareTimearray = array_filter($cityciqurechangetime, function($n) use ($iniFirstLegEndTime) {
        return $n >= $iniFirstLegEndTime;
    });

    return $CitySquareTimearray;
}

var_dump(gettime());

The callback within array_filter returns a boolean to inform array_filter which items to include in the $CitySquareTimearray array.

Comments

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.