1

i have created an arry like

<?php
    $arr['00']=NULL;
    $arr['30']=NULL;
    $arr['31']=0.013;
    $arr['33']=0.013;
    $arr['34']=0.013;
    $arr['351']=NULL;
    $arr['39']=0.013;
    $arr['421']=NULL;
    $arr['44']=0.009;
    $arr['44GG']=0.009;
    $arr['44IM']=0.009;
    $arr['44JE']=0.009;
    $arr['49']=0.013;
    $arr['52']=NULL;
    $arr['55']=NULL;
    $arr['61']=NULL;
    $arr['62']=NULL;
    $arr['63']=NULL;
    $arr['86']=NULL;
    $arr['90']=NULL;
    $arr['90CC']=NULL;
    $arr['94']=NULL;
    $arr['994']=NULL;
    $arr['95']=0.02;

//before move

foreach( $arr as $i => $a)
{
    echo $i.'-'.$a.'<br>';
}

echo '<br>';

uasort($arr, function($a, $b){
    if ($a == NULL) return 1;
    if ($b == NULL) return -1;
    return 0;
});

//after move

foreach( $arr as $i => $a)
{
    echo $i.'-'.$a.'<br>';
}

I want to move NULL value down without sorting index and get following output

31-0.013
33-0.013
34-0.013
39-0.013
44-0.009
44GG-0.009
44IM-0.009
44JE-0.009
49-0.013
95-0.02
00-
30-
351-
421-
52-
55-
61-
62-
63-
86-
90-
90CC-
94-
994-

but i am getting

95-0.02
44-0.009
44JE-0.009
44IM-0.009
44GG-0.009
49-0.013
39-0.013
34-0.013
33-0.013
31-0.013
94-
90CC-
90-
86-
63-
62-
421-
55-
52-
30-
351-
994-
00-
61-

I can do it using loop and recereate array. but i need to do using uasort. please help me on it Thanks in advance. I am beginner. please excuse me if i am asking very simple question

user231410
  • 129
  • 1
  • 15
  • 1) You're not considering the case where both `$a` and `$b` are `null`, 2) you want a stable sort on top of that: http://stackoverflow.com/a/17365409/476 – deceze Jul 07 '16 at 09:01

1 Answers1

0

Short solution using array_filter function and array union operator +:

// $arr is your initial array
$null_items = array_filter($arr, "is_null");
$notnull_items = array_filter($arr);

$result = $notnull_items + $null_items;
print_r($result);

The output:

Array
(
    [31] => 0.013
    [33] => 0.013
    [34] => 0.013
    [39] => 0.013
    [44] => 0.009
    [44GG] => 0.009
    [44IM] => 0.009
    [44JE] => 0.009
    [49] => 0.013
    [95] => 0.02
    [00] => 
    [30] => 
    [351] => 
    [421] => 
    [52] => 
    [55] => 
    [61] => 
    [62] => 
    [63] => 
    [86] => 
    [90] => 
    [90CC] => 
    [94] => 
    [994] => 
)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105