1

I need to sort my multidimensional array by a value in the child array. In the array example below, I need to sort the parent arrays by child value "revenue_certificate".

function custom_sort($a, $b) {
    return strcmp($a['revenue_certificate'], $b['revenue_certificate']);
}

usort($data_array, 'custom_sort');

I feel like I'm almost there, but where I simply don't understand is how to reference the child array value of "revenue_certificate".

Array
(
    [0] => Array
        (
            [company_id] => 130
            [company_name] => Eureka / Brookings
            [revenue_certificate] => 3
            [revenue_cash] => 33
            [average_sale] => 0
            [total_orders] => 0
            [certificates_per_order] => -1
            [revenue_per_certificate] => -1
            [visible_items] => 25
            [retail_value] => -1
            [average_discount] => -1
            [new_advertisers] => 1
            [emails_harvested] => 1
            [new_customers] => 1
        )

    [1] => Array
        (
            [company_id] => 82
            [company_name] => Big Deals Across America
            [revenue_certificate] => 1
            [revenue_cash] => 0
            [average_sale] => 0
            [total_orders] => 0
            [certificates_per_order] => -1
            [revenue_per_certificate] => -1
            [visible_items] => 1
            [retail_value] => -1
            [average_discount] => -1
            [new_advertisers] => 0
            [emails_harvested] => 0
            [new_customers] => 0
        )

    [2] => Array
        (
            [company_id] => 134
            [company_name] => Fergus Falls, MN
            [revenue_certificate] => 2
            [revenue_cash] => 20
            [average_sale] => 0
            [total_orders] => 0
            [certificates_per_order] => -1
            [revenue_per_certificate] => -1
            [visible_items] => 128
            [retail_value] => -1
            [average_discount] => -1
            [new_advertisers] => 129
            [emails_harvested] => 2
            [new_customers] => 1
        )

)

Thanks for any help.

2
  • 2
    your code looks ok, What's the problem? Do you get an error? Commented Jan 28, 2016 at 23:48
  • No error. When running code as originally posted the array is sorted by "revenue_certificate" but it's not treating the value of "revenue_certificate" as a number, rather a string. For instance, this might be the resulting order: 0, 10, 11000, 125, 1350, 20 Commented Jan 28, 2016 at 23:54

3 Answers 3

1

Don't use strcmp :)

function custom_sort($a, $b) {
    return $a['revenue_certificate'] - $b['revenue_certificate'];
}

usort($data_array, 'custom_sort');

custom_sort should return a negative, 0, positive value when $a < $b, $a == $b, $a < $b respectively (just as strcmp does BTW).

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

1 Comment

Worked great! Follow-up though, how does this actually work? It looks like it's just subtracting one array value from another.
0

I believe you need to use ksort instead

Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.

2 Comments

ksort looks like it sorts the keys instead of OP's problem where he needs to sort his array based on values of a particular key. Am I misunderstanding the problem at hand and/or your solution?
check out the note by DavidG, it seems like this is what OP needs. I could be mistaken though
0
Array ( [0] => Array ( [India] => 2 ) [1] => Array ( [Canada] => 1 ) [2] => Array ( [USA] => 3 ) )

also sort this desc order according to country value

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.