I have a string of 7 numbers in an array looks like 4,1,2,56,7,9,10 however sometimes these elements are empty ,,,56,7,9,10 for example. What I would like to do is reorder the array so it looks like 56,7,9,10,,,
Asked
Active
Viewed 48 times
0
-
see [this SO post](http://stackoverflow.com/questions/3654295/remove-empty-array-elements). I would go with array_filter myself. – jaudette Dec 20 '12 at 21:37
-
So you don't want to *remove* the empty ones, but move them to the end? – animuson Dec 20 '12 at 21:37
2 Answers
0
try this:
$null_counter = 0;
foreach($array as $key => $val) {
if($val == null) {
$null_counter++;
unset($array[$key]);
}
}
for($x=1;$x<=$null_counter;$x++) {
$array[] = null;
}

Pitchinnate
- 7,517
- 1
- 20
- 37
0
Use unset in loop to remove null value and shift the values Up.
foreach($yourarray as $key=>$val )
{
if($yourarray[$key] == '')
{
unset($yourarray[$key]);
}
}

Vaibs
- 2,018
- 22
- 29