RE: [PHP] sorting multi-dimensional array where array elements are structs [simple class]
Okay. For some reason, usort messes things up when you have a multi-dimensional array (or at least mine). When I commented out the call to usort, the information printed, but out of order. When I called the usort, Im not sure what happened, but all of the elements in my array were blank. What I had to do is copy each row to a temporary array, perform the usort, then I just printed from there - I didn't even store it back into the original multi-dimensional array (although I'm sure I could do that if it were necessary). At any rate, things are working as I need them to. Thanks again. -Original Message- From: Michael Sims [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 11, 2003 7:19 PM To: [EMAIL PROTECTED] Cc: Erin Fry Subject: Re: [PHP] sorting multi-dimensional array where array elements are structs [simple class] On Tue, 11 Feb 2003 08:27:57 -0600, you wrote: Thanks for the reply. I had already tried usort previously. For some reason, there is no data for the array fields at all in the cmp function - not sure why. Does anyone know? All help is appreciated! Thanks. Can you post a short code sample which illustrates your problem? --- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.449 / Virus Database: 251 - Release Date: 1/27/2003 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.449 / Virus Database: 251 - Release Date: 1/27/2003 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sorting multi-dimensional array where array elements are structs [simple class]
On Mon, 10 Feb 2003 16:23:10 -0600, you wrote: Hi. Im working with a multidimensional array where the data in the array is a class (struct). All the information is being stored correctly, but I need to sort each column (AA0, BA0, etc. see below) by the fieldNo portion of the struct. class fieldClass { var $fieldNo; var $srcLineNo; var $data; } [...] Although I've never personally used it, I believe usort() is what you need: http://www.php.net/manual/en/function.usort.php You would need to create a callback function, something like: function cmp($a, $b) { if ($a-fieldNo == $b-fieldNo) { return 0; } return ($a-fieldNo $b-fieldNo) ? 1 : -1; } usort($structArray, 'cmp'); Thanks for the reply. I had already tried usort previously. For some reason, there is no data for the array fields at all in the cmp function - not sure why. Does anyone know? All help is appreciated! Thanks. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.449 / Virus Database: 251 - Release Date: 1/27/2003 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multi-dimensional array where array elements are structs [simple class]
On Tue, 11 Feb 2003 08:27:57 -0600, you wrote: Thanks for the reply. I had already tried usort previously. For some reason, there is no data for the array fields at all in the cmp function - not sure why. Does anyone know? All help is appreciated! Thanks. Can you post a short code sample which illustrates your problem? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sorting multi-dimensional array where array elements are structs [simple class]
On Mon, 10 Feb 2003 16:23:10 -0600, you wrote: Hi. Im working with a multidimensional array where the data in the array is a class (struct). All the information is being stored correctly, but I need to sort each column (AA0, BA0, etc. see below) by the fieldNo portion of the struct. class fieldClass { var $fieldNo; var $srcLineNo; var $data; } [...] Although I've never personally used it, I believe usort() is what you need: http://www.php.net/manual/en/function.usort.php You would need to create a callback function, something like: function cmp($a, $b) { if ($a-fieldNo == $b-fieldNo) { return 0; } return ($a-fieldNo $b-fieldNo) ? 1 : -1; } usort($structArray, 'cmp'); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php