Re: sorting multi-dimensional array where array elements are structs [simple class]

From: Date: Tue, 11 Feb 2003 02:37:19 +0000
Subject: Re: sorting multi-dimensional array where array elements are structs [simple class]
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-135146@lists.php.net to get a copy of this message
On Mon, 10 Feb 2003 16:23:10 -0600, you wrote: >Hi. > >I’m 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');

« previous php.general (#135146) next »