PHPfilter: in a bit over my head: is there a native function to sort an array by a word which isn't always in a fixed position, or do I need to create one? (And if I need to create one, is this the best way to do it?)
I have a form set up to receive reports and process them, breaking each report into an array populated with individual records, and then splitting the records off into other arrays based on different criteria.
I need to be able to sort the final arrays based on words which are not necessarily in a fixed position in each element.
A simplified example: Here's an array in its original form (assume each element is on its own line):
JAdventure Stilton
JFantasy Abbott
JFantasy Osborne
JFantasy Toriyama
JFiction Blume
JFiction Walt
JHist Gonzalez
JHist Osborne
JMystery Erickson
JMystery Sobol
JScifi Dezago
JScifi Star
L.P. JFiction Park
L.P. JHist Tripp
Here's the form I need it to be in:
JFantasy Abbott
JFiction Blume
JScifi Dezago
JMystery Erickson
JHist Gonzalez
JFantasy Osborne
JHist Osborne
L.P. JFiction Park
JMystery Sobol
JScifi Star
JAdventure Stilton
JFantasy Toriyama
L.P. JHist Tripp
JFiction Walt
Is there a native function to do this kind of sorting?
Assuming the answer is no, would the following be the most efficient way to manage what I need done? (Forgive my pseudocode; I'm still just trying to wrap my head around the problem.)
1: RM "^JAdventure " if present:
a. foreach($jmedia as $key => $value)
b. if preg_match "^JAdventure " (
i. preg_replace "^JAdventure " with nothing
ii. $jmedia[$key][1] = "JAdventure "
)
2. sort
3. foreach($jmedia as $key =>$value)
a. if $jmedia[$key][1] then (
i. add $jmedia[$key][1] as a string to the start of $jmedia[$key]
ii. remove $jmedia[$key][1]
)
boyfriend: usort
boyfriend: but really, wtf, use a database
posted by Stephanie Duy at 2:07 PM on June 21, 2011