Page 1 of 1

Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 1:19 pm
by JAB Creations
I'm having trouble stripping empty keys from a numeric array, here is what I currently have...

Code: Select all

<?php
$pizza  = "piece1\npiece2\n\n\npiece3\npiece4\npiece5 piece6";
$pieces = explode("\n",$pizza);
 
 foreach($pieces as $key)
 {
  if ($key=="") {unset($pieces[$key]);}
 }
 
print_r($pieces);
?>

Re: Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 1:35 pm
by jackpf

Code: Select all

<?php
$pizza  = "piece1\npiece2\n\n\npiece3\npiece4\npiece5 piece6";
$pieces = explode("\n",$pizza);
 
 foreach($pieces as $key => $value)
 {
  if ($value=="") {unset($pieces[$key]);}
 }
 
print_r($pieces);
?>
How about that?

Re: Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 2:09 pm
by JAB Creations
Sweet, thanks Jack! I thought I was sort of close. :mrgreen:

Re: Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 2:30 pm
by jackpf
Lol yeah. No problem.

Re: Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 3:37 pm
by Ollie Saunders
Also consider:

Code: Select all

preg_split("/\n/", $toSplit, -1, PREG_SPLIT_NO_EMPTY);

Re: Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 3:52 pm
by jackpf
Wow, I didn't know about that. Nice one.

Re: Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 4:50 pm
by John Cartwright
There is also array_filter() if you already have the array built. I.e.,

Code: Select all

$pieces = array_filter(explode("\n",$pizza));
Although since you are both creating the array and filtering it, I would recommend Ollie's advise.

Re: Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 5:35 pm
by Ollie Saunders
The downside to mine is having to supply the split as a regular expression. For \n it's no big deal but if you had to call preg_quote() I think John's solution would be preferable.

Re: Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 5:50 pm
by JAB Creations
Thanks for the replies and especially to Ollie for his solution... I'm actually trying to figure out a reliable way to benchmark Ollie's code against pytrin's solution. I wasn't aware earlier that the two things I was trying to do could be done in pretty much a single action, so to speak, otherwise I wouldn't have made a second thread.

I was reading various things online such an article on getrusage that it made no sense while I was messing with it on my LAMP server. I generally avoid regular expressions though I really should benchmark things instead of working with a biased policy against this or that. After I have some dinner I'm going to construct both and run each solution in a loop a thousand times and see if I can get an accurate idea of how the contrast. I'm just primarily looking for speed. Any suggestions on existing benchmark methods are welcome...here is the code I've used in the past that I'll try both solutions with...

Code: Select all

<?php
$time_start = microtime(true);
// Sleep for a while
usleep(1000);
 
$time_end = microtime(true);
$time = $time_end - $time_start;
 
// Do stuff here
 
echo 'Rendered in ';
//echo substr($time, 0, 7);
echo round($time, 7);
echo ' seconds.';
?>

Re: Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 5:57 pm
by Eran
Those code snippets will never be the bottleneck in your application, so choosing them based on performance is useless. Choose the one that are you most comfortable with and achieves what you set out to do. There are many ways to skin a cat

Re: Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 6:22 pm
by jackpf
Yeah. The time difference would be negligible.

Re: Stripping empty keys/line breaks from numeric array?

Posted: Mon Aug 17, 2009 6:32 pm
by John Cartwright
some Guru wrote:premature optimization is the root of all evil
:P