5

I have an array:

$example = array();

$example ['one']   = array('first' => 'blue',
                           'second' => 'red');

$example ['two']   = array('third' => 'purple',
                           'fourth' => 'green');

$example ['three'] = array('fifth' => 'orange',
                           'sixth' => 'white');

Based on some input to the function, I need to change the order of the example array before the foreach loop processes my output:

switch($type)

case 'a':
//arrange the example array as one, two three
break;

case 'b':
//arrange the example array as two, one, three
break;

case 'c':
//arrange the example array in some other arbitrary manner
break;

foreach($example as $value){
        echo $value;
}

Is there an easy way to do this without re-engineering all my code? I have a pretty indepth foreach loop that does the processing and if there was an easy way to simple re-order the array each time that would be really helpful.

2
  • 2
    What is the purpose of your "arbitrary array sorting?" Are you simply trying to randomize the order? Or, do you specifically want to shift the items around in a parametric fashion? Commented Feb 26, 2013 at 21:23
  • I guess the point is more that I need to sort them how I want and that doesn't fall into any readily available sorting criteria like alphabetical or numerical...I need it to be sorted precisely how I tell it to be sorted. Commented Feb 26, 2013 at 21:55

4 Answers 4

2

You can use array_multisort for your permutation. I assume that you know the permutation and don't need to derive it from the key names. Say, you want the order two, three, one, then create a reference array like that:

$permutation = array(3, 1, 2);

Meaning: first item goes to position 3, second item to position 1, third item to position 2

Then, after the switch, permute:

array_multisort($permutation, $example);

This will sort the $permutation array and apply the same order to $example.

Sign up to request clarification or add additional context in comments.

3 Comments

since I am not using numeric keys will the permutation array work? This seems to be the route for me to take because you are absolutely right in that I do know the permutation I want for each situation.
It has nothing to do with the keys and array_multisort preserves non-numeric keys, so: yes, it will work
Yep this was the way to do it, thanks again. Took a bit to wrap my head around the permutations (haven't had to use them in awhile) but ultimately this was the simplest method given I knew the order of each case.
2

You are not going to find a silver bullet answer here. You are going to probably need to write your own function for use with uksort().

uksort($example, function ($a, $b) use $type {
    switch($type) {
        case 'a':
            if ($a === 'one' || $b === 'three') return 1;
            if ($a === 'three' || $b === 'one') return -1;
            if ($a === 'two' && $b === 'three') return 1;
            return -1;
            break;
        // and so on...
    }
});

Comments

0

You need to write a custom sort function for each case and then use usort. The first case could just use sort. The arbitrary sort needs to be logic you define.

Comments

0

I see more ways to solve this, but not knowing more about I don't know what is better.

  1. Make a pre-score (make a numeric value for each element, calculated in your "pretty in-depth foreach loop"), then sort the array by it using it as the main and single criteria.

  2. If your criteria s are based on only numeric fields, ASC/DESC use http://php.net/manual/en/function.array-multisort.php

  3. make custom sorting functions for each case (http://www.php.net/manual/en/function.usort.php)

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.