| 0 comments ]


function trim_array($array){
$newarray= array();
$i = 0;
foreach ($array as $key => $value) {
if (!empty($value)) {
$newarray[$i] = $value;
$i++;
}
}
}

function trim_array_restorekey($array){
foreach ($array as $key => $value) {
if (empty($value)) {
unset($array[$key]);
}
}

return $array;
}

$sourseArray=array("0"=>"Black",
"1"=>"blue",
"2"=>"",
"3"=>"green",
"4"=>"",
"5"=>"pink",
);

echo "Sourse array
";

print_r($sourseArray);

echo "
Remove Empty locations of array with out key restoration
";

print_r(trim_array($sourseArray));
echo "
Remove Empty locations of array with key restoration
";

print_r(trim_array_restorekey($sourseArray));
?>
output :

Source array

array("0"=>"Black",
"1"=>"blue",
"2"=>"",
"3"=>"green",
"4"=>"",
"5"=>"pink",
);

Remove Empty locations of array with out key restoration

array("0"=>"Black",
"1"=>"blue",
"2"=>"green",
"3"=>"pink",
);

Remove Empty locations of array with key restoration

array("0"=>"Black",
"1"=>"blue",
"3"=>"green",
"5"=>"pink",
);

0 comments

Post a Comment

Please put your comments here. your questions, your suggestions, also what went wrong with me.