![]() ![]() the previous example does neither, especially as it does a lot of repetitive things in each iteration, such as asigning the character order constant, rebuilding it into an array and suchįor example, the string comparison could be written as such : ![]() Recursion is only good if it simplifies your code or your understanding of the concept. in the specific case, using recursion to compare strings has a huge performance impact while a simple loop would suffice and be faster and more simple. Genrally speaking, any recursion can be reimplemented using simple iteration. Regarding the recursive sorting function above: Note that this might not be the fastest thing on earth and it hasn't been tested very much yet but I hope it's useful for someone. $family -> sortMembers (array( "firstName" => "ASC", "age" => "DESC", "hairColor" => "ASC" )) įoreach ( $family -> members as $member )Įcho $member -> firstName. $family -> open () // this will also fetch all members Uksort ( $this -> details, array( $this, "_objSort" )) $direction = "DESC" ? $this -> sortDirections = - 1 : $this -> sortDirections = 1 $diff = $this -> _objSort ( $a, $b, $i ) įoreach ( $sortFields as $field => $direction ) If ( $diff = 0 & isset( $this -> sortFields )) $diff = strnatcmp ( $this -> details -> $field, $this -> details -> $field ) * $direction To sort the family members AFTER fetching them with the database query, you can use the functions _objSort and sortMembers which will sort the "members" array by key using it's properties (for space reasons I didn't include the methods used to open the records): To identify members, I'm using their memberId as the key of the array e.g. Each of those objects prepresents a record in a database related to the family (by it's familyId). a class "family" has family members stored as an array of objects. I'm using classes as an abstraction for querying records in a database and use arrays of objects to store records that have an 1 to n relationship. (about sorting an array of objects by their properties in a class - inspired by webmaster at zeroweb dot org at usort function) Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |