vincej Posted March 13, 2012 Share Posted March 13, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/258806-why-wont-my-array_filter-work/ Share on other sites More sharing options...
Psycho Posted March 13, 2012 Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258806-why-wont-my-array_filter-work/#findComment-1326710 Share on other sites More sharing options...
salathe Posted March 13, 2012 Share Posted March 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258806-why-wont-my-array_filter-work/#findComment-1327017 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.