Jump to content

[SOLVED] Unset empty array values


jcbarr

Recommended Posts

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

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

?>

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.