Hi,
Question: I have this text file that I want to sort according to cart letter (cart A, cart B ...). I was looking though the different array sort options and did not really see anything that met my needs. How would I sort, by cart letter, the following type of information keeping all relevant data together? Or do I have to do this using a database instead of a text file.
a:3:{s:4:"name";s:10:"Bill Gates";s:4:"cart";s:6:"Cart A";s:4:"date";s:10:"11/07/2008";}
a:3:{s:4:"name";s:10:"Mike Bibby";s:4:"cart";s:6:"Cart D";s:4:"date";s:10:"11/08/2006";}
a:3:{s:4:"name";s:8:"Joe Blow";s:4:"cart";s:6:"Cart A";s:4:"date";s:10:"11/08/2006";}
a:3:{s:4:"name";s:12:"Mike Schmeer";s:4:"cart";s:6:"Cart B";s:4:"date";s:10:"11/08/2006";}
I was thinking about the natural sort option but don't know how I could get this to work.
Could I read all the relevant info into an array? So for example after reading the text file, unserializing it and looping through it, could I then store all the info into an array such as $info and then use natsort($info['cart']). Would that work or is that not a way you can use natsort()?
As always, thank you so much for your help.
And yes, I know I should be using mysql for a database but I'm not quite there yet, though it looks like I may have to go that way soon.
Array sorting question
Moderator: General Moderators
- Cameri
- Forum Commoner
- Posts: 87
- Joined: Tue Apr 12, 2005 4:12 pm
- Location: Santo Domingo, Dominican Republic
Let's say $carts is the array that contains all the carts after you read the file.
Let's define a function that will compare the cart names...
And let's order the $carts array using this function as a callback and the uksort() function:
Now let's see the $carts array ordered...
NOTE: I did not test any of this.
Let's define a function that will compare the cart names...
Code: Select all
function cmp($cart_a, $cart_b){
return strcmp($cart_a['cart'],$cart_b['cart']);
}Code: Select all
uksort($carts,"cmp");Code: Select all
var_dump($carts);