0

Whats the best way to get each of the "Carrier"'s lowest rate?

Array
(
    [0] => Array
        (
            [Method] => SMART_POST
            [Title] => Smart Post
            [Price] => 8.58
            [Carrier] => fedex
            [Carrier Title] => Fedex
            [Description] =>
        )

    [1] => Array
        (
            [Method] => GROUND_HOME_DELIVERY
            [Title] => Home Delivery
            [Price] => 12.34
            [Carrier] => fedex
            [Carrier Title] => Fedex
            [Description] =>
        )

    [2] => Array
        (
            [Method] => FEDEX_EXPRESS_SAVER
            [Title] => Express Saver
            [Price] => 16.25
            [Carrier] => fedex
            [Carrier Title] => Fedex
            [Description] =>
        )

    [3] => Array
        (
            [Method] => FEDEX_2_DAY
            [Title] => 2 Day
            [Price] => 20.49
            [Carrier] => fedex
            [Carrier Title] => Fedex
            [Description] =>
        )

    [4] => Array
        (
            [Method] => PRIORITY_OVERNIGHT
            [Title] => Priority Overnight
            [Price] => 32.17
            [Carrier] => fedex
            [Carrier Title] => Fedex
            [Description] =>
        )

    [5] => Array
        (
            [Method] => 4
            [Title] => Standard Post
            [Price] => 8.76
            [Carrier] => usps
            [Carrier Title] => United States Postal Service
            [Description] =>
        )

    [6] => Array
        (
            [Method] => 1
            [Title] => Priority Mail 3-Day
            [Price] => 10.25
            [Carrier] => usps
            [Carrier Title] => United States Postal Service
            [Description] =>
        )

    [7] => Array
        (
            [Method] => 3
            [Title] => Priority Mail Express 2-Day
            [Price] => 38.8
            [Carrier] => usps
            [Carrier Title] => United States Postal Service
            [Description] =>
        )

)

With the above array the result would be

fedex Smart Post 8.58 usps Standard Post 8.76

5
  • start with a foreach loop Commented Jul 16, 2014 at 4:20
  • I have some working code, it's just horribly inefficient and hard to debug, I'm asking for the best option. Commented Jul 16, 2014 at 4:28
  • I didn't understand your requirement. You told you need the "lowest" rate. The "lowest" rate is only one value, but you are telling in the explanation for 2 values. Am I missing something? Commented Jul 16, 2014 at 4:31
  • Lowest based off of carrier. Commented Jul 16, 2014 at 4:36
  • foreach, create array for each carrier, sort that Commented Jul 16, 2014 at 4:37

1 Answer 1

1

Try this

$arr = Array
(
    0 => Array
        (
            'Method' => 'SMART_POST',
            'Title' => 'Smart Post',
            'Price' => '8.58',
            'Carrier' => 'fedex',
            'Carrier Title' => 'Fedex',
            'Description' =>''
        ),

    1 => Array
        (
            'Method' => 'GROUND_HOME_DELIVERY',
            'Title' => 'Home Delivery',
            'Price' => '12.34',
            'Carrier' => 'fedex',
            'Carrier Title' => 'Fedex',
            'Description' =>''
        ),

    2 => Array
        (
            'Method' => 'FEDEX_EXPRESS_SAVER',
            'Title' => 'Express Saver',
            'Price' => '16.25',
            'Carrier' => 'fedex',
            'Carrier Title' => 'Fedex',
            'Description' =>''
        ),

    3 => Array
        (
            'Method' => 'FEDEX_2_DAY',
            'Title' => '2 Day',
            'Price' => '20.49',
            'Carrier' => 'fedex',
            'Carrier Title' => 'Fedex',
            'Description' =>''
        ),

    4 => Array
        (
            'Method' => 'PRIORITY_OVERNIGHT',
            'Title' => 'Priority Overnight',
            'Price' => '32.17',
            'Carrier' => 'fedex',
            'Carrier Title' => 'Fedex',
            'Description' =>''
        ),

    5 => Array
        (
            'Method' => 4,
            'Title' => 'Standard Post',
            'Price' => '8.76',
            'Carrier' => 'usps',
            'Carrier Title' => 'United States Postal Service',
            'Description' =>''
        ),

    6 => Array
        (
            'Method' => 1,
            'Title' => 'Priority Mail 3-Day',
            'Price' => '10.25',
            'Carrier' => 'usps',
            'Carrier Title' => 'United States Postal Service',
            'Description' =>''
        ),

    7 => Array
        (
            'Method' => 3,
            'Title' => 'Priority Mail Express 2-Day',
            'Price' => '38.8',
            'Carrier' => 'usps',
            'Carrier Title' => 'United States Postal Service',
            'Description' =>''
        )

);
usort($arr, function($a, $b) {
    return $a['Price'] - $b['Price'];
});
print_r($arr);
Sign up to request clarification or add additional context in comments.

2 Comments

A good start, but only does the lowest. I need the lowest of each. I guess I could make two arrays from the data based off the carrier.
function sortr($a, $b) { return $a['Price'] - $b["Price"]; } usort($rates, "sortr"); $fst = array_shift($rates); while ($fst['Carrier']==$rates[0]['Carrier']){ array_shift($rates); }

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.