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.

Link to comment
Share on other sites

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 );

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

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.