0

Below is my array , i want to sort it based on [attributes]->[0]->[value] how can i do it please guide me. i tried using sort but how to make it work for value of child array. Pls guide me its new for me.

[549246] => Array
        (
            [info] => Array
                (
                    [price] => $281.00
                    [qty] => 10
                    [prod_id] => 549246
                )

            [attributes] => Array
                (
                    [0] => Array
                        (
                            [code] => sizes
                            [label] => Size
                            [value] => 25mg
                            [attribute_id] => 191
                            [option_id] => 26
                        )

                )

        )

    [549245] => Array
        (
            [info] => Array
                (
                    [price] => $75.00
                    [qty] => 10
                    [prod_id] => 549245
                )

            [attributes] => Array
                (
                    [0] => Array
                        (
                            [code] => sizes
                            [label] => Size
                            [value] => 5mg
                            [attribute_id] => 191
                            [option_id] => 24
                        )

                )

        )

1 Answer 1

1

try below;

function customer_sort ($a, $b) {

if ((int)$a['attributes'][0]['value'] == (int)$b['attributes'][0]['value']) {
    return 0;
}

return (int)$a['attributes'][0]['value'] > (int)$b['attributes'][0]['value'] ? 1 : -1;

}
uasort($arr, 'customer_sort');
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.