I am attempting to create a multisort functionality using an array created from a parsed CSV file.
Here is an example output of what this array looks like
Array (
[1] => Array
(
[attribute_set] => Area Rug
[baybin] => 19B
[refnumber] =>
[sku] => 3K34302300
[rug_size] => 5 x 8
[collection] => Suncoast
[style] => Brooke
[stylenumber] => 3K343
[colorname] => Sage
[colornumber] => 02300
[weight] => 15.0000
[sold_on] => Overstock
[shape] => Rectangle
[yarn] => Olefin
[construction] => Woven
[qty] => 58.0000
)
[16] => Array
(
[attribute_set] => Area Rug
[baybin] => 33C
[refnumber] => 746916
[sku] => 3K46500100
[rug_size] => 5 x 8
[collection] => Grace Bay
[style] => Beige
[stylenumber] => 3K465
[colorname] => Beige/natural
[colornumber] => 00100
[weight] => 27.0000
[sold_on] => Amazon
[shape] => Rectangle
[yarn] =>
[construction] => Woven
[qty] => 0.0000
)
My initial idea
I would be sending the sort and filter parameters through $_GET.
The format would look something like
products.php?C:Suncoast,s:,Z:8 x 10
Using that format, I would first multisort the entire contents of the array created from the CSV, shown below.
For instance, a captial C
would stand for Collection Descending, small s
for Size Ascending.
The colon after the letter indicates the value I would filter by.. AFTER the array is sorted.
My goal is to have these sorts combined; just like in SQL where you can ORDER BY several columns.
So, the only solution I came up with was to create a dynamic string by looping through the parsed GET variables and creating a single string that I would call like so
eval("array_multisort($evalstring);");
As far as I know, array_multisort
can only be called one time if I expect a multiple sort functionality i.e sort by collection first, then that by qty.
Perhaps?
Perhaps I am going about this all wrong? Is there a better way to accomplish this? It's usage would be for sorting products (as well as filtering them) to narrow the list down.
First, I would create some convenience functions that help to build the parts that go into
array_multisort()
:Then build the arguments:
And finally call the sort function:
Parsing
Here's an idea for a parsing routine: