Page 1 of 1
How to sort an array according to certain structure?
Posted: Fri Apr 16, 2010 3:43 pm
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!
Re: How to sort an array according to certain structure?
Posted: Fri Apr 16, 2010 3:45 pm
by Benjamin
How would you do it in plain english?
Re: How to sort an array according to a certain structure?
Posted: Fri Apr 16, 2010 4:07 pm
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

Re: How to sort an array according to certain structure?
Posted: Fri Apr 16, 2010 4:17 pm
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.
Re: How to sort an array according to certain structure?
Posted: Fri Apr 16, 2010 4:29 pm
by Benjamin
What I meant was, what steps would you take to sort them in plain english.
Re: How to sort an array according to certain structure?
Posted: Fri Apr 16, 2010 4:35 pm
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.
Re: How to sort an array according to certain structure?
Posted: Fri Apr 16, 2010 4:41 pm
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.
Re: How to sort an array according to certain structure?
Posted: Fri Apr 16, 2010 4:47 pm
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

Re: How to sort an array according to certain structure?
Posted: Sat Apr 17, 2010 7:47 am
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);
}