jcbarr Posted March 20, 2007 Share Posted March 20, 2007 I have a file that I dumped in to an array. I then used strip_tags to remove all the tags from each element. My question is this, how do I get rid of the empy ones? I tried many different methods that I found on php.net and nothing would work, I just kept coming up with the same array over and over again. The code is very basic right now; <?php $stats = file("all_stat.htm"); foreach ($stats as $line){ $newStats[] = strip_tags($line); } echo "<pre>"; print_r($newStats); echo "</pre>"; ?> Click this link to actually see a print of the array http://cbl-baseball.com/stats.php Link to comment https://forums.phpfreaks.com/topic/43565-solved-unset-empty-array-values/ Share on other sites More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 <?php $stats = file("all_stat.htm"); foreach ($stats as $line){ $line = strip_tags($line); if (trim($line) != "") $newStats[] = $line; } echo "<pre>"; print_r($newStats); echo "</pre>"; ?> Does that work? Link to comment https://forums.phpfreaks.com/topic/43565-solved-unset-empty-array-values/#findComment-211578 Share on other sites More sharing options...
Kerblam Posted March 20, 2007 Share Posted March 20, 2007 I'd do this: <?php $stats = file("all_stat.htm"); foreach ($stats as $line){ if (!empty($line) { $newStats[] = strip_tags($line); } } echo "<pre>"; print_r($newStats); echo "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/43565-solved-unset-empty-array-values/#findComment-211580 Share on other sites More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 There is one problem with that Kerblam. The strip_tags will remove data. IE: $line may equal this: $line = "<td> </td>"; $line = strip_tags($line); print $line; // will print " "; It should work the same if it was done like this: <?php $stats = file("all_stat.htm"); foreach ($stats as $line){ $line = strip_tags($line) if (!empty($line) $newStats[] = $line; } echo "<pre>"; print_r($newStats); echo "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/43565-solved-unset-empty-array-values/#findComment-211581 Share on other sites More sharing options...
kenrbnsn Posted March 20, 2007 Share Posted March 20, 2007 You can do what frost110 suggests of you can use the array_filter() function after the array has been created: <?php <?php $stats = file("all_stat.htm"); $newstats = array(); foreach ($stats as $line){ $newStats[] = strip_tags($line); } $newerstats = array_filter($newstats); echo '<pre>' . print_r($newerStats,true) . '</pre>'; ?> Ken Link to comment https://forums.phpfreaks.com/topic/43565-solved-unset-empty-array-values/#findComment-211583 Share on other sites More sharing options...
jcbarr Posted March 20, 2007 Author Share Posted March 20, 2007 That did it Frost, sorry Kerblam, didn't try yours when I found that his worked. Now just to find out how to actually do something with this stuff... I'm hoping to be able to put it in to a database or something, thanks for the help. I'm sure I will be posting here again soon enough, I'm still a noob when it comes to manipulating arrays. Link to comment https://forums.phpfreaks.com/topic/43565-solved-unset-empty-array-values/#findComment-211584 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.