2

I have an array like this in PHP. I what to ordered by amount ascending:

Array
(
    [0] => Array
        (
            [trans_id] => PR58EC68AFE8B0F3186
            [bill_number] => 1
            [order_date] => 2017-04-11
            [amount] => 800
            [trx_type] => purchase
        )

    [1] => Array
        (
            [trans_id] => PR58EC68AFE8B0F3186
            [bill_number] => 1
            [order_date] => 2017-04-11
            [amount] => 150
            [trx_type] => purchase-paid
        )

    [2] => Array
        (
            [trans_id] => PR58EC68AFE8B0F3186
            [bill_number] => 1
            [order_date] => 2017-04-11
            [amount] => 100
            [trx_type] => purchase-paid
        )

    [3] => Array
        (
            [trans_id] => BL58EC68EC4BCA18805
            [bill_number] => 1
            [order_date] => 2017-04-11
            [amount] => 2000
            [trx_type] => bill
        )
4
  • 1
    Welcome to SO. Please step first to HELP center, then read How to Ask Question and provide a MCVE : Minimal, Complete, and Verifiable Example. If people around can easily read and understand what you mean, or what the problem is, they'll be more likely willing to help :) Commented Apr 11, 2017 at 7:29
  • 1
    Welcome to Stackoverflow. Have a look at stackoverflow.com/help/how-to-ask - as you can see, we're here to help you with specific programming problems, not to code for you. Show us what you've tried so far, share your thoughts and we'll help you... But nobody is going to write code for you. Commented Apr 11, 2017 at 7:29
  • Possible duplicate of PHP: sort array by value Commented Apr 11, 2017 at 7:31
  • php.net/array_multisort example #3 Commented Apr 11, 2017 at 7:35

1 Answer 1

1

you can use usort() built in function with your custom function like this

    <?php
$yourarray = array(
    0 => array(
        'bill_number' => 3,
        'amount' => 100
    ),
    1 => array(
        'bill_number' => 4,
        'amount' => 50
    ),
    2 => array(
        'bill_number' => 5,
        'amount' => 150
    ),
);

function sortByOrder($a, $b)
{
    return $a['amount'] - $b['amount'];
}

usort($yourarray, 'sortByOrder');
echo "<pre>";
print_r($yourarray);
?>

then output will be

Array
(
    [0] => Array
        (
            [bill_number] => 4
            [amount] => 50
        )

    [1] => Array
        (
            [bill_number] => 3
            [amount] => 100
        )

    [2] => Array
        (
            [bill_number] => 5
            [amount] => 150
        )

)

for more information

http://php.net/manual/en/function.usort.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.