Re: [PHP] check if any element of an array is not empty
2008/4/30 Nathan Nobbe [EMAIL PROTECTED]: On Wed, Apr 30, 2008 at 2:58 PM, Richard Heyes [EMAIL PROTECTED] wrote: but I was thinking if there is the function does that. array_filter(). Note this: If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed. http://uk3.php.net/manual/en/function.array-filter.php i dont really see how that gets him the answer without at least checking the number of elements in the array after filtering it w/ array_filter; Because an empty array evaluates to false when cast to bool (which is implicit in a conditional). So this should work fine: if (array_filter($myArray)) { // only do this if there's stuff worth bothering with in myArray } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] check if any element of an array is not empty
i dont really see how that gets him the answer without at least checking the number of elements in the array after filtering it w/ array_filter; which if he wanted to reuse in several places would make sense to write a simple function for anyway.. Yes, on both counts. -- Richard Heyes ++ | Access SSH with a Windows mapped drive | |http://www.phpguru.org/sftpdrive| ++ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] check if any element of an array is not empty
At 3:36 PM -0500 4/30/08, afan pasalic wrote: hi, as a result of one calculation I'm receiving an array where elements could be 0 or date (as string -mm-dd hh:ii:ss). I have to check if any of elements of the array is date or if all elements of the array is 0? If I try array_sum($result) I'll get 0 no matter what (0 + $string = 0). I know I can do something like: foreach($result as $value) { if ($value !=0) { $alert = true; } } or if (in_array($result, '-')) { $alert = true; } but I was thinking if there is the function does that. thanks for any help. -afan You can create an empty array and check to see if the difference via array_diff. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] check if any element of an array is not empty
hi, as a result of one calculation I'm receiving an array where elements could be 0 or date (as string -mm-dd hh:ii:ss). I have to check if any of elements of the array is date or if all elements of the array is 0? If I try array_sum($result) I'll get 0 no matter what (0 + $string = 0). I know I can do something like: foreach($result as $value) { if ($value !=0) { $alert = true; } } or if (in_array($result, '-')) { $alert = true; } but I was thinking if there is the function does that. thanks for any help. -afan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] check if any element of an array is not empty
On Wed, Apr 30, 2008 at 2:36 PM, afan pasalic [EMAIL PROTECTED] wrote: hi, as a result of one calculation I'm receiving an array where elements could be 0 or date (as string -mm-dd hh:ii:ss). I have to check if any of elements of the array is date or if all elements of the array is 0? If I try array_sum($result) I'll get 0 no matter what (0 + $string = 0). I know I can do something like: foreach($result as $value) { if ($value !=0) { $alert = true; } } or if (in_array($result, '-')) { $alert = true; } but I was thinking if there is the function does that. i don think theres a function for that; and to be performance conscience, if just one occurrence of a date is all you need to know its not all 0's then break out of the loop once youve found that. foreach($result as $value) { if ($value !== 0) { $alert = true; break; } } -nathan
Re: [PHP] check if any element of an array is not empty
but I was thinking if there is the function does that. array_filter(). Note this: If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed. http://uk3.php.net/manual/en/function.array-filter.php -- Richard Heyes ++ | Access SSH with a Windows mapped drive | |http://www.phpguru.org/sftpdrive| ++ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] check if any element of an array is not empty
yup! that's the one :D thanks richard Richard Heyes wrote: but I was thinking if there is the function does that. array_filter(). Note this: If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed. http://uk3.php.net/manual/en/function.array-filter.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] check if any element of an array is not empty
On Wed, Apr 30, 2008 at 2:58 PM, Richard Heyes [EMAIL PROTECTED] wrote: but I was thinking if there is the function does that. array_filter(). Note this: If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed. http://uk3.php.net/manual/en/function.array-filter.php i dont really see how that gets him the answer without at least checking the number of elements in the array after filtering it w/ array_filter; which if he wanted to reuse in several places would make sense to write a simple function for anyway.. function isSomethingInArray($array) { if(count(array_filter($array)) 0) { return true; } else { return false; } } -nathan