Jump to content

how can I arrange an array I got from database?


shedokan

Recommended Posts

I have a table in the database wich stores some numbers, then I calculate those numbers and combine them.

then I take all the numbers and the combined number and put them into an array wich stores more numbers and more combined numbers in different id's, now I want to sort the combined numbers wich are in a multi array, the array now looks like this:

<?php
Array
(
    [1] => Array
        (
            [num1] => 1
            [num2] => 1.5
            [num3] => 1.125
            [combinedNum] => 3.625
        )

    [2] => Array
        (
            [num1] => 1.5
            [num2] => 0.875
            [num3] => 0.875
            [combinedNum] => 3.25
        )

    [3] => Array
        (
            [num1] => 13.5
            [num2] => 2.375
            [num3] => 3.625
            [combinedNum] => 19.5
        )
)
?>

 

what is the simplest way of sorting this array by the combinedNum?

 

thanks, I really appreciate your knowledge.

You can't change your database query to have an ORDER BY column and just get the data out of the database already sorted?  That's usually the easiest and most efficient method.

 

If you insist on the array_multisort() method, you want to model your code on example #3 in the array_multisort documentation.

 

<?php

$combinedNum = array();
foreach( $your_array as $key => $val ) {
  $combinedNum[$key] = $val['combinedNum'];
}

array_multisort( $combinedNum, SORT_ASC, $your_array );

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.