Re: [PHP] Re: unset empty elements in an array

2004-07-12 Thread Justin French
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


Re: [PHP] Re: unset empty elements in an array

2004-07-12 Thread Paul Bissex
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'71W 42°19'42N

--
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

2004-07-12 Thread Curt Zirzow
* 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

2004-07-12 Thread Torsten Roehr
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

2004-07-12 Thread Justin French
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