-1

i have an array of date in php like

Array
(
    [0] => Array
        (
            [month] => April-2014
            [total_booking] => 2
        )

    [1] => Array
        (
            [month] => May-2014
            [total_booking] => 5
        )

    [2] => Array
        (
            [month] => June-2014
            [total_booking] => 25
        )

    [3] => Array
        (
            [month] => October-2013
            [total_booking] => 1
        )

    [4] => Array
        (
            [month] => July-2014
            [total_booking] => 4
        )

)

i want to find the minimum and maximum month from this array. currently i am using foreach() function and comparing each element. but i think this is not ideal if the array is bigger. is there any other option? expected output from the above array

minimum:October-2013
maximum :July-2014

update now i have converted this array to following structure

  Array
(
    [October-2013] => 5
    [April-2014] => 2
    [June-2014] => 17
    [August-2014] => 1
    [May-2014] => 4
    [July-2014] => 8
)

then for finding minimum and maximum by

natcasesort($testKeys = array_keys($newarray));

                    echo 'Min: ' . $testKeys[0] . ', max: ' . $testKeys[count($testKeys) - 1] . '<br />';

but i am getting output

Min: October-2013, max: July-2014  .

expected maximum is August-2014.

can any one find the solution?

Nisham Mahsin
  • 1,399
  • 5
  • 19
  • 43

1 Answers1

1

For that array structure, iterating into it to find the max and min is the only way to go.

Unless you can make a 1D array that looks like this:

array(201406, 201302, 201312)

You can then use PHP's built-in max and min functions.

For that 1D array above and your original array, you can use the booking ID as the index. So that when you find out the max/min value from the 1D array, you can reference the associated booking details using the array index.

Chris Lam
  • 3,526
  • 13
  • 10