jnymris Posted November 25, 2011 Share Posted November 25, 2011 Solution function get_clients_list(){ $portal = $this->load->database('clientportal', TRUE); $query = $portal->get('cplinktable')->result(); foreach ($query as $client) $client->client_name=$this->get_username($client->autotaskid)->account_name; $result=usort($query,array($this,'sort_clients')); die(var_dump($result)); return $query; } function sort_clients($a,${ if($a->client_name > $b->client_name) return 1; else if ($a->client_name < $b->client_name) return -1; else return 0; } QuestionOkay I'm really having problems with trying to sort an array which contain objects Here's my output from my query $client is the array thats being shown. array(49) {[0]=> object(stdClass)#19 (6 { ["id"]=> int(3) ["external_id"]=> int(29735292) ["asset_db"]=> string(18) "WaspTrackAsset_SFT" ["business_mgr"]=> NULL ["tech_mgr"]=> NULL ["client_name"]=> string(22) "Client Name 1 } [1]=> object(stdClass)#20 (6) { ["id"]=> int(4) ["external_id"]=> int(27529753) ["asset_db"]=> string(17) "WaspTrackAsset_TS" ["business_mgr"]=> NULL ["tech_mgr"]=> NULL ["client_name"]=> string(28) "Client Name 2" } [2]=> object(stdClass)#21 (6) { ["id"]=> int(5) ["external_id"]=> int(27529736) ["asset_db"]=> string(17) "WaspTrackAsset_HC" ["business_mgr"]=> NULL ["tech_mgr"]=> NULL ["client_name"]=> string(14) "Client Name 3" } [3]=> object(stdClass)#22 (6) { ["id"]=> int(14) ["external_id"]=> int(29701765) ["asset_db"]=> string(18) "WaspTrackAsset_ATA" ["business_mgr"]=> NULL ["tech_mgr"]=> NULL ["client_name"]=> string(15) "Client Name 4" }.. (You get the point right?)} now what i'm trying to do is arrange the arrange for the client_name with the most alabetical name to be $client[0] without loosing the external_id, asset_db etc. i have tried ksort() and looked at usort(). I've google search for this but nothing seems to explaining in clearly. Arrays and objects are not my strong point however I am starting to understand them much more.Would greatly if someone could take the time to give an example AND explain (it's pointless giving the just the code) Thanks Very MuchJohnny (The reason i cannot use an order_by is due to the full function: function get_clients_list(){ $portal = $this->load->database('clientportal', TRUE); $query = $portal->get('cplinktable')->result(); foreach ($query as $client) $client->client_name=$this->get_username($client->autotaskid)->account_name; //die(var_dump($query)); return $query; } ) Link to comment Share on other sites More sharing options...
Ingolme Posted November 25, 2011 Share Posted November 25, 2011 You can use the usort() function as you mentioned. The function takes an argument defining a callback which sorts the element. The callback compares two elements of the function and you must return a value based on whether the first should go before, after, or either of the positions (A negative value, a positive value or 0, respectively) So you call usort like this: usort($my_array, 'custom_sort');Then you create the custom_sort() function.I'll consider $my_array your list of objects $my_array = get_clients_list();usort($my_array, 'custom_sort');function custom_sort($a, $ { // The < and > operators used on strings check whether a string is alphabetically greater or less than another value if($a->client_name > $b->client_name) { // A is greater than B return 1; } else if( $a < $b ) { // A is less than B return -1; } else { // A and B have the same value return 0; }} Link to comment Share on other sites More sharing options...
jnymris Posted November 25, 2011 Author Share Posted November 25, 2011 EDITThanks Very Much i got this workingI was trying to create a new variable using: $result=usort($query,'sort_clients'); instead of using usort($query,'sort_clients'); Can i ask, Where does the $b come from cause we are onyl passing one variable through? Thanks So i've tried two ways to do the uSort i'm using a MCV design method so i tried: (This is all being done in the model because this is reference more than once and it always to be in alphabetical order. function get_clients_list(){ $portal = $this->load->database('clientportal', TRUE); $query = $portal->get('cplinktable')->result(); foreach ($query as $client) $client->client_name=$this->get_username($client->autotaskid)->account_name; $result=usort($query,$this->sort_clients); die(var_dump($result)); return $query; } function sort_clients($a,${ if($a->client_name > $b->client_name) return 1; else if ($a->client_name < $b->client_name) return -1; else return 0; } Error returned was:Message: usort() expects parameter 2 to be a valid callback, no array or string given and retruned NULL when pringin $result so i tried: function get_clients_list(){ function sort_clients($a,${ if($a->client_name > $b->client_name) return 1; else if ($a->client_name < $b->client_name) return -1; else return 0; } $portal = $this->load->database('clientportal', TRUE); $query = $portal->get('cplinktable')->result(); foreach ($query as $client) $client->client_name=$this->get_username($client->autotaskid)->account_name; $result=usort($query,'sort_clients'); die(var_dump($result)); return $query; } Error Returned:bool(true) Link to comment Share on other sites More sharing options...
Ingolme Posted November 25, 2011 Share Posted November 25, 2011 You have to pass a string with the name of the sort function. You should make the function global and not part of the object. I don't know how to reference object functions in a callback, I don't think it's possible. Also, the forum turned my $b into an uppercase $B in the line:function sort_clients($a, $b )Be sure to fix that. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 25, 2011 Share Posted November 25, 2011 You use an array to specify an object method. The first element is a reference to the object, and the second element is a string of the method name: usort($query, array($this, 'sort_clients')); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.