How to sort an array according to certain structure?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crimsontwo
Forum Newbie
Posts: 6
Joined: Fri Apr 16, 2010 3:41 pm

How to sort an array according to certain structure?

Post by crimsontwo »

Hi,

I have 2 arrays:

ARR1 = "S, M, L, XL, XXL, 2XL, 3XL, 4XL"
ARR2 = "XL, M, L"

I want to sort ARR2 so that it follows the structure of ARR1. The end result should be: ARR2 = "M, L, XL".

Can't figure out how to do that :)

Thanks in advance!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How to sort an array according to certain structure?

Post by Benjamin »

How would you do it in plain english?
crimsontwo
Forum Newbie
Posts: 6
Joined: Fri Apr 16, 2010 3:41 pm

Re: How to sort an array according to a certain structure?

Post by crimsontwo »

OK, let me put it this way.

(a) I am selling shirt sizes XS, S, M, L, XL, XXL, 2XL, 3XL
(b) When I query a given shirt in the database, I get an array with available sizes that is not necessarily in the order described in (a)
(c) Not all shirts are available in all possible sizes. What I am trying to achieve is to sort whatever sizes that are available according to my desired
order described in (a). Unfortunately I can't sort them by name since, for instance, L will go before M. Hence, I am stuck.

I hope this is plain enough :)
solid
Forum Commoner
Posts: 28
Joined: Wed Aug 12, 2009 11:56 am

Re: How to sort an array according to certain structure?

Post by solid »

I feel like there must be a function that handles this, but since I cannot find one, this will work for your use:

Code: Select all

/**
 * Returns $values array sorted by $index values
 *
 * @param array $index sort order index
 * @param array $values to be sorted
 * @return array of $values sorted based on $index
 */
function sortArray($index, $values)
{
   $output = array();
   foreach ($index as $key)
   {
      if (in_array($key, $values)) $output[] = $key;
   }
   return $output;
}
Edited to move 'return' line outside of foreach loop, lol.
Last edited by solid on Fri Apr 16, 2010 4:32 pm, edited 2 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How to sort an array according to certain structure?

Post by Benjamin »

What I meant was, what steps would you take to sort them in plain english.
crimsontwo
Forum Newbie
Posts: 6
Joined: Fri Apr 16, 2010 3:41 pm

Re: How to sort an array according to certain structure?

Post by crimsontwo »

Benjamin wrote:What I meant was, what steps would you take to sort them in plain english.
Not sure that I understand your question :)

My desired order:

ARR_1[0] = "XS"
ARR_1[1] = "S"
ARR_1[2] = "M"
ARR_1[3] = "L"
ARR_1[4] = "XL"
ARR_1[5] = "2XL"
ARR_1[6] = "3XL"

I then query DB and get:

ARR_2[0] = "L"
ARR_2[1] = "M"
ARR_2[2] = "XS"
ARR_2[3] = "2XL"
ARR_2[4] = "S"

I would then like to sort ARR_2 so that it ends up being:

ARR_2[0] = "XS"
ARR_2[1] = "S"
ARR_2[2] = "M"
ARR_2[3] = "L"
ARR_2[4] = "2XL"

ARR_1 would serve as an index.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How to sort an array according to certain structure?

Post by Benjamin »

I know what order you want them to be in, and I have also written the code to do it.

What I am asking is for you to tell me how you would do this in plain english. If you can't figure out how to sort these in english, or in your head, you certainly won't be able to ever write code to do it.
crimsontwo
Forum Newbie
Posts: 6
Joined: Fri Apr 16, 2010 3:41 pm

Re: How to sort an array according to certain structure?

Post by crimsontwo »

Benjamin wrote:I know what order you want them to be in, and I have also written the code to do it.

What I am asking is for you to tell me how you would do this in plain english. If you can't figure out how to sort these in english, or in your head, you certainly won't be able to ever write code to do it.
@solid: thanks for the code.

@Benjamin: in terms of me being unable to "sort these in English", I did and showed you my preferred result. Please re-phrase your question or we'll just drop this topic :)
User avatar
JAY6390
Forum Newbie
Posts: 20
Joined: Sat Apr 17, 2010 6:51 am
Location: UK

Re: How to sort an array according to certain structure?

Post by JAY6390 »

Here's a simple function that will sort them for you if you don't have them in an array by default

Code: Select all

echo size_sort('XS,S,M,L,XL,XXL', 'XL,M,S');

function size_sort($original_list, $sortable) {
    $original = explode(',', $original_list);
    $original = array_flip($original);
    $sortable = explode(',', $sortable);
    $ret = array();
    foreach($sortable as $v) {
        if(isset($original[$v]))
        $ret[$original[$v]] = $v;
    }
    ksort($ret);
    return implode(',', $ret);
}
Post Reply