Stripping empty keys/line breaks from numeric array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Stripping empty keys/line breaks from numeric array?

Post 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);
?>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post by JAB Creations »

Sweet, thanks Jack! I thought I was sort of close. :mrgreen:
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

Lol yeah. No problem.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

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

Post by Ollie Saunders »

Also consider:

Code: Select all

preg_split("/\n/", $toSplit, -1, PREG_SPLIT_NO_EMPTY);
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

Wow, I didn't know about that. Nice one.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

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

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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.';
?>
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

Yeah. The time difference would be negligible.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post by John Cartwright »

some Guru wrote:premature optimization is the root of all evil
:P
Post Reply