Jump to content

Why won't my Array_filter work ?


vincej

Recommended Posts

Hi - I have looked at a dozen examples and I think my syntax is correct, yet for some reason, I must be doing it wrong as it won't work. My filter_array() produces the exact same output as a print_r() which makes me think that there is something is wrong with the function, but I can't see what it could be. I'm using PHP 5.3.8

 

Many Many Thanks for your help !

 

The Array Contents from a print_r()

Array ( [0] => Array ( [pickupid] => 5 [pickuplocation] => Banff 
[date1] => 1329980400 [date2] => 1333260000 [date3] => 1334469600 [date4] => 1335852000 ) ) 

 

 

The Array_Filter()

function greaterThanNow ($i){
$now = time();
if ($i > $now)  {
    	return true;
  	}
}
$filtereddates = (array_filter(	$deliverydates,"greaterThanNow"));
print_r($filtereddates);

 

Link to comment
Share on other sites

Your input is a multidimensional array. array_filter is meant for a single-dimensional array. There are some examples in the user contributed notes for a recursive usage of the function. But, to be honest, the format of your array seems off to me. The 'dates' should be in their own sub-array instead of date1, date2, etc.

Link to comment
Share on other sites

vincej, could you provide an example of the array you're trying to filter and an example of the array that you want to finish with. That way we can see what it is that you're trying to do.

 

Currently, your array has one item. That one item is an array with the pickup and date information.  array_filter() will call your function once for each item within it (since your array only has one item, it'll only be called once).  Your function looks to be wanting to compare integer times (the date* sub-items?) when in reality $i will be an array with the pickup and date information.

 

Maybe some more description of where the array comes from, and the general purpose of having the array and the need for filtering it, would help too.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.