Re: [PHP] Re: unset empty elements in an array
On 13/07/2004, at 3:14 AM, Paul Bissex wrote: How about array_filter()? From the docs: "If the callback function is not supplied, array_filter() will remove all the entries of input that are equal to FALSE." $a = array ('a' => 'foo', 'b' => '', 'c' => null, 'd' => 99, 'e' => 0); print_r (array_filter ($a)); // Output: Array ( [a] => foo [d] => 99 ) As a previous poster noted, though, this will only work for you if "0" and the empty string et al. are not significant in your application. Nice idea Paul, will try it out :) --- Justin French http://indent.com.au -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: unset empty elements in an array
"Curt Zirzow" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > * Thus wrote Justin French: > > On 12/07/2004, at 6:38 PM, Thomas Seifert wrote: > > > > My view of internal functions is that they solve common, repetitive > > problems. Sure, some of those problems are quite complex, but others > > are not. A perfect example would be array_walk() -- it can be solved > > in a "one liner" too: > > > > foreach ($in as $k => $v ) { $in[$k] = myFunction($v); } > > > > ... but someone decided it was useful as an internal function. My hope > > was that there was such a function to delete empty array elements > > already defined in PHP, but since it appears there isn't, I'll just > > keep including my own from a library file. > > The problem with your function you want is that it is very > specific, the name would have to be something like: > > unset_array_items_only_if_val_is_empty($array); What about: array_unset_empty_values() or array_remove_empty_values() :-) Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: unset empty elements in an array
* Thus wrote Justin French: > On 12/07/2004, at 6:38 PM, Thomas Seifert wrote: > > My view of internal functions is that they solve common, repetitive > problems. Sure, some of those problems are quite complex, but others > are not. A perfect example would be array_walk() -- it can be solved > in a "one liner" too: > > foreach ($in as $k => $v ) { $in[$k] = myFunction($v); } > > ... but someone decided it was useful as an internal function. My hope > was that there was such a function to delete empty array elements > already defined in PHP, but since it appears there isn't, I'll just > keep including my own from a library file. The problem with your function you want is that it is very specific, the name would have to be something like: unset_array_items_only_if_val_is_empty($array); Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: unset empty elements in an array
On Tue, 13 Jul 2004 01:57:48 +1000, Justin French <[EMAIL PROTECTED]> wrote: > [...] My hope > was that there was such a function to delete empty array elements > already defined in PHP, but since it appears there isn't, I'll just > keep including my own from a library file. How about array_filter()? From the docs: "If the callback function is not supplied, array_filter() will remove all the entries of input that are equal to FALSE." $a = array ('a' => 'foo', 'b' => '', 'c' => null, 'd' => 99, 'e' => 0); print_r (array_filter ($a)); // Output: Array ( [a] => foo [d] => 99 ) As a previous poster noted, though, this will only work for you if "0" and the empty string et al. are not significant in your application. pb -- paul bissex, e-scribe.com -- database-driven web development 413.585.8095 69.55.225.29 01061-0847 72°39'71"W 42°19'42"N -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: unset empty elements in an array
On 12/07/2004, at 6:38 PM, Thomas Seifert wrote: Really what do you need an internal function for something simple like that? It may be simple, but it's 60 characters I have to type over and over, or a user defined function I have to include() from a library file (and I have to ensure that library file is up to date on 30+ web applications). My view of internal functions is that they solve common, repetitive problems. Sure, some of those problems are quite complex, but others are not. A perfect example would be array_walk() -- it can be solved in a "one liner" too: foreach ($in as $k => $v ) { $in[$k] = myFunction($v); } ... but someone decided it was useful as an internal function. My hope was that there was such a function to delete empty array elements already defined in PHP, but since it appears there isn't, I'll just keep including my own from a library file. --- Justin French http://indent.com.au -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: unset empty elements in an array
Daniel Kullik wrote: Justin French wrote: Hi, Looking for a one-liner to delete all empty elements in an array. I know I can do it with a foreach loop, but I'm hoping that I've missed an existing function in the manual which may already do this, or a simple one-liner to replace the foreach. $v) { if(empty($v)) { unset($in[$k]); } } ?> --- Justin French http://indent.com.au Though it's not really a one-liner: [code] while ($key = array_search('', $in)) unset($in[$key]); [/code] For more infos on array_seach(): http://www.php.net/array_search Daniel A note I forgot to addi in my previous posting: You ought to think over what you consider "empty" since empty() would for example return true if the checked variable contained the integer 0 or the string '0'. My posted line of code recognizes only an empty string as "empty". You might want to take a look at this: http://www.php.net/manual/en/types.comparisons.php Daniel -- WWE e-commerce IT GmbH Eiffestrasse 462, D-20537 Hamburg Tel.: +49-40-2530659-0, Fax: +49-40-2530659-50 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: unset empty elements in an array
Justin French wrote: Hi, Looking for a one-liner to delete all empty elements in an array. I know I can do it with a foreach loop, but I'm hoping that I've missed an existing function in the manual which may already do this, or a simple one-liner to replace the foreach. $v) { if(empty($v)) { unset($in[$k]); } } ?> --- Justin French http://indent.com.au Though it's not really a one-liner: [code] while ($key = array_search('', $in)) unset($in[$key]); [/code] For more infos on array_seach(): http://www.php.net/array_search Daniel -- WWE e-commerce IT GmbH Eiffestrasse 462, D-20537 Hamburg Tel.: +49-40-2530659-0, Fax: +49-40-2530659-50 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: unset empty elements in an array
On Mon, 12 Jul 2004 15:33:53 +1000, Justin French wrote: > Hi, > > Looking for a one-liner to delete all empty elements in an array. I > know I can do it with a foreach loop, but I'm hoping that I've missed > an existing function in the manual which may already do this, or a > simple one-liner to replace the foreach. > > foreach($in as $k => $v) { > if(empty($v)) { > unset($in[$k]); > } > } > ?> Sure, a one-liner: foreach ($in as $k => $v ) { if(empty($v)) unset($in[$k]); } ;). Really what do you need an internal function for something simple like that? thomas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php