Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
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? –  user1477388 Feb 26 '13 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. –  absentx Feb 26 '13 at 21:55

4 Answers 4

up vote 2 down vote accepted

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.

share|improve this answer
    
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. –  absentx Feb 26 '13 at 23:20
    
It has nothing to do with the keys and array_multisort preserves non-numeric keys, so: yes, it will work –  fschmengler Feb 26 '13 at 23:21
    
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. –  absentx Feb 26 '13 at 23:46

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...
    }
});
share|improve this answer

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.

share|improve this answer

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.