0

I defined a myArr = ("Apr","Mar","May"). Is there a sorting function to sort the array members by nature month sequence otherwise by alphabet.

The array members variables can will be changed at runtime.

I want it output followed Mar, Apr, May using foreach loop.

3 Answers 3

2

You could use usort() and create your own function to compare months.

function monthCompare($a, $b)
{
  $months = array('jan' => 1, 'feb' => 2..._);
  if($a == $b)
  {
    return 0;
  }
  return ($months[$a] > $months[$b]) ? 1 : -1;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Useful, because you can also add any number of variations on the name - ie array('jan' => 1, 'january' => 1... and it will be handled correctly. Note that you should also include an isset check above and do a alphabetical sort if not present.
1

Bulding on @preinheimer's answer, here is a version that will do a sequential sort if the name doesn't exist:

$data = array("Apr", "Mar", "Jan", "Feb", "ddd", "aaa", "ccc");

function monthCompare($a, $b) {
    $a = strtolower($a);
    $b = strtolower($b);
    $months = array(
        'jan' => 1,
        'feb' => 2,
        'mar' => 3,
        'apr' => 4,
        'may' => 5
    );
    if($a == $b)
        return 0;
    if(!isset($months[$a],$months[$b]))
        return $a > $b;
    return ($months[$a] > $months[$b]) ? 1 : -1;
}

usort($data, "monthCompare");

echo "<pre>";
print_r($data);

Returns:

Array
(
    [0] => aaa
    [1] => ccc
    [2] => ddd
    [3] => Jan
    [4] => Feb
    [5] => Mar
    [6] => Apr
)

However - this highlights logic flaw with your question. You've asked that it be sorted by month sequence otherwise by alphabet. The problem with that is you haven't sufficiently defined the sort order in a way that can be reliabliy replicated. For example, using the above algorythm and the array "ddd", "aaa", "ccc", "Apr", "Mar", "Jan", "Feb" (ie, same elements) gives the result:

Array
(
    [0] => aaa
    [1] => Jan
    [2] => Feb
    [3] => Mar
    [4] => Apr
    [5] => ccc
    [6] => ddd
)

Both answers are correct according to your request, so you need to define the sort requirement in more detail.

Comments

0

No. there is no such a function. But.... you still can solve your problem gracefully. You should associate months names with numbers (1 is for Jan, 2 is for Feb and so on). This is the most common approach in programming.

Define your array as:

$myArr = array(1=>"Jan", 2=>"Feb" ... )

And then you can sort your $myArr by keys (ksort):

ksort($myArr);
var_export($myArr);

That would sort your array in ascendant order by keys.

6 Comments

difficult, since "array members variables will be changed at runtime.".
@Hamish Make a copy of initial array, so you can work with it without making any changes to the former. I would not say it is difficult. It's absolutely, not. It's just... hmmmm... inconvenient and kind of "not natural", and controversial comparing to Python, for example, where dictionaries (PHP's arrays) assorting is done much prettier.
a copy of the initial array doesn't tell you how to sort the new array.
Ammm.. I did not get that :) I'm sorry. Maybe my English is not good enough. I like this (my) approach because it does not lead to redundant code like in your example. And what if I've changed names of months from short to full names like 'Jan' => 'January' ? Next time I will change it to 'Jan.' and to 'J A N U A R Y'? My approach will still work, and you will need to create new comparison function or edit existing one, ane then you will edit comparison functions name for every usort call... Ahhh... I personally don't like to write more code then needed growing complexity for simple things.
Your solution is to manually add a sorting key to the data in the array. The code isn't redundant, it actually solves the problem as asked rather than just restating it in another form.
|

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.