Jump to content

PHP code sort not working


neeko

Recommended Posts

I have an array in which score is a key and it has value like 2.0 , 7.8 , 9.8 etc.

 

I want to sort that array by score ascending.

array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC);
print_r($allcomps);

print_r shows without sorting.

Link to comment
Share on other sites

12 minutes ago, neeko said:

I have an array in which score is a key and it has value like 2.0 , 7.8 , 9.8 etc.

 

I want to sort that array by score ascending.


array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC);

print_r($allcomps);

print_r shows without sorting.

<?php
   $allcomps = array(
      array(
        'score' => 2.0,
      ),
      array(
        'score' =>  7.8,
        
      ),
      array(
        'score' =>  9.8,
      )
    );
    
    array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC);
    print_r ($allcomps);
?>

 

Link to comment
Share on other sites

<?php
   $allcomps = array(
      array(
        'score' => 7.0,
      ),
      array(
        'score' =>  7.8,
        
      ),
      array(
        'score' =>  2.8,
      )
    );
    
    array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC);
    print_r ($allcomps);
?>

Not working.

Output: Array ( [0] => Array ( [score] => 7 ) [1] => Array ( [score] => 7.8 ) [2] => Array ( [score] => 2.8 ) )

Link to comment
Share on other sites

10 minutes ago, neeko said:

<?php
   $allcomps = array(
      array(
        'score' => 7.0,
      ),
      array(
        'score' =>  7.8,
        
      ),
      array(
        'score' =>  2.8,
      )
    );
    
    array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC);
    print_r ($allcomps);
?>

Not working.

Output: Array ( [0] => Array ( [score] => 7 ) [1] => Array ( [score] => 7.8 ) [2] => Array ( [score] => 2.8 ) )

 

never mind its not working for me also...after chaning the input

 

Link to comment
Share on other sites

27 minutes ago, neeko said:

<?php
   $allcomps = array(
      array(
        'score' => 7.0,
      ),
      array(
        'score' =>  7.8,
        
      ),
      array(
        'score' =>  2.8,
      )
    );
    
    array_multisort(array_column($allcomps, "score"), SORT_STRING, SORT_ASC);
    print_r ($allcomps);
?>

Not working.

Output: Array ( [0] => Array ( [score] => 7 ) [1] => Array ( [score] => 7.8 ) [2] => Array ( [score] => 2.8 ) )

 

21 minutes ago, dasari4kntr said:

 

never mind its not working for me also...after chaning the input

 

 

try this...it is working..

<?php
   $allcomps = array(
      array(
        'score' => '7.8',
      ),
      array(
        'score' =>  '2.0',
        
      ),
      array(
        'score' =>  '9.8',
      )
    );
    

    $a = array_column($allcomps, "score");
    usort($a,"my_sort");
    print_r ($a);
    
    function my_sort($a,$b) {
        if ($a==$b) return 0;
        return ($a<$b)?-1:1;
    }
?>

e5CqxMR.jpeg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...