From: Richard L. <ce...@l-...> - 2009-02-21 04:54:32
|
I would use strlen on it, rather than just ($string) personally... If $string happens to contain just "0" then ($string) would probably not behave as you desire. For the case of pulling 1|2|3 from the DB, I'd suggest you consider a DB data structure that doesn't make you use PHP explode in the first place :-) SQL arrays or bit-masked INT or ??? Another option is to just use the array you have, but skip any empty values as you loop through them. One reason why explode does what is does is so that it is the exact reverse of implode (and vice versa) What would you expect these to output: implode('|', array('foo', 'bar', 'baz')); implode('|', array('foo')); implode('|', array('')); As you can see, if that last one is to do what you expect, and implode/explode are supposed to be exactly opposite, then explode('') must return an array with a single empty string element. On Fri, February 20, 2009 3:00 pm, Arlo Leach wrote: > Hi folks, > > Consider the following PHP code, which explodes an empty string: > > $string = ""; > $array = explode("|", $string); > print_r($array); > print "Length: ".count($array); > > I would expect this to output an empty array: > > Array ( ) Length: 0 > > But it actually outputs: > > Array ( [0] => ) Length: 1 > > Does anyone else find this odd? It has bitten me a few times lately, > when I > test the existence of data with a count(), or try to process data with > a FOR > loop. I end up writing this a lot to get the expected results: > > $array = ($string) ? explode(",", $string) : array() ; > > I was considering logging a bug report but wondered what your thoughts > were. > > Cheers, > -Arlo > > _______________________________ > > Arlo Leach > 773.769.6106 > http://arlomedia.com > > > > > ------------------------------------------------------------------------------ > Open Source Business Conference (OSBC), March 24-25, 2009, San > Francisco, CA > -OSBC tackles the biggest issue in open source: Open Sourcing the > Enterprise > -Strategies to boost innovation and cut costs with open source > participation > -Receive a $600 discount off the registration fee with the source > code: SFAD > http://p.sf.net/sfu/XcvMzF8H > _______________________________________________ > chiPHPug-discuss mailing list > chi...@li... > https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss > -- Some people ask for gifts here. I just want you to buy an Indie CD for yourself: http://cdbaby.com/search/from/lynch |