0

I have an array

           Array
                (
                    [0] => Array
                        (                                
                            [order_id] => 1318
                            [code] => shipping
                            [title] => UK Shipping  (Weight: 0.00kg)
                            [value] => 10.2000                                
                        )

                    [1] => Array
                        (

                            [order_id] => 1318
                            [code] => sub_total
                            [title] => Sub-Total
                            [value] => 4.7000                                
                        )

                    [2] => Array
                        (                                
                            [order_id] => 1318
                            [code] => coupon
                            [title] => Coupon (10P)
                            [value] => -0.4700                                
                        )

                    [3] => Array
                        (                                
                            [order_id] => 1318
                            [code] => tax
                            [title] => VAT (20%)
                            [value] => 2.8860
                            [sort_order] => 8
                        )

                    [4] => Array
                        (                                
                            [order_id] => 1318
                            [code] => total
                            [title] => Total
                            [value] => 17.3160                                
                        )
                    )

I want to swap the array index. I want to swap the array index when [code] => coupon to [code] => sub_total, If coupon is available. I want the position of coupon to above the subtotal. I want the position of sub_total to above the vat. How is it possible? Please help me.

1
  • 4
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. Commented Apr 18, 2016 at 15:39

2 Answers 2

1

You can define a custom sort order array that puts each of the possible values of code in the order you want.

$custom_sort_order = array(
    'shipping' => 1,
    'coupon' => 2,
    'sub_total' => 3,
    'tax' => 4,
    'total' => 5);

then you can use that custom sort order in the comparison function of usort to get the array items in your desired order.

usort($your_array, function($x, $y) use ($custom_sort_order) {

    // find the sort number for each item
    $x = $custom_sort_order[$x['code']];
    $y = $custom_sort_order[$y['code']];

    // do the comparison
    if ($x == $y) return 0;
    return $x - $y;
});
Sign up to request clarification or add additional context in comments.

Comments

0

To avoid nominating the entire array's row order and running a sorting algorithm upon the whole array, just nominate the values to determine the two targeted rows, make a reference assignment, swap the reference values, then break the loop. This will have an optimal time complexity.

Code: (Demo)

$toSwap = ['sub_total', 'coupon'];
foreach ($array as &$row) {
    if (in_array($row['code'], $toSwap)) {
        $refs[] =& $row;
        if (count($refs) === 2) {
            [$refs[0], $refs[1]] = [$refs[1], $refs[0]];
            break;
        }
    }
}
var_export($array);

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.