Trim and remove empty key & value in php array

On December 4, 2009, in CakePHP, Oscommerce, PHP, Tips, Tricks, by phpsolutions

Here is the PHP function that will remove empty key => value in PHP array…

function array_trim($sv)
{
$s = 0;
$svn = null;
$c = count($sv);
for($i = 0; $i < $c; $i++)
{
if($sv[$i] != “”){
$svn[$s++] = trim($sv[$i]);
}
}
return $svn;
}

$svarray = array(‘   ‘, ‘svlinux’, ‘phpsolutions’, ‘lamp’, ”);
$svarray = array_trim($svarray);
echo ‘<pre>’;
print_r($svarray);
echo ‘</pre>’;

Output:

Array(
[0] => svlinux
[1] => phpsolutions
[2] => lamp
)

2 Responses to Trim and remove empty key & value in php array

  1. Anonymous says:

    Wow, that’s an incredibly nice read!

  2. Anonymous says:

    I am really thankful to the author with this post for producing this lovely and informative article live for us. We really appreciate ur effort. Maintain the favorable work. . . .

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.