note 39886 added to function.sort

From: Date: Sat, 14 Feb 2004 01:34:02 +0000
Subject: note 39886 added to function.sort
Groups: php.notes 
Request: Send a blank email to php-notes+get-65127@lists.php.net to get a copy of this message
Please discard my previous post[perhaps an admin can deleted it for me? :-)]. It turns out the original script I implemented it in didn't seem to work properly. After more development, I did find a method that works as theorized in my previous post. I also added in a new function. What it does is allows you to add in multiple key names, it will then sort them in that order, the purpose is, is that if the first one is the same throughout, it will jump to the next key. Here's the script: <?php // +-- First let's set up the multidimensional array. $array[0][username] = "user_name"; $array[0][password] = "pass_word"; $array[0][account_type] = "admin"; $array[1][username] = "eman_resu"; $array[1][password] = "drow_ssap"; $array[1][account_type] = "user"; $array[2][username] = "php_roxs"; $array[2][password] = "yesitdoes"; $array[2][account_type] = "admin"; // +-- Run it through the function. $sorted = mu_sort($array, "password,account_type"); // +-- Print out the results[just for this test] print_r($sorted); // +-- Here's the function: function mu_sort ($array, $key_sort) { // start function $key_sorta = explode(",", $key_sort); $keys = array_keys($array[0]); // sets the $key_sort vars to the first for($m=0; $m < count($key_sorta); $m++){ $nkeys[$m] = trim($key_sorta[$m]); } $n += count($key_sorta); // counter used inside loop // this loop is used for gathering the rest of the // key's up and putting them into the $nkeys array for($i=0; $i < count($keys); $i++){ // start loop // quick check to see if key is already used. if(!in_array($keys[$i], $key_sorta)){ // set the key into $nkeys array $nkeys[$n] = $keys[$i]; // add 1 to the internal counter $n += "1"; } // end if check } // end loop // this loop is used to group the first array [$array] // into it's usual clumps for($u=0;$u<count($array); $u++){ // start loop #1 // set array into var, for easier access. $arr = $array[$u]; // this loop is used for setting all the new keys // and values into the new order for($s=0; $s<count($nkeys); $s++){ // set key from $nkeys into $k to be passed into multidimensional array $k = $nkeys[$s]; // sets up new multidimensional array with new key ordering $output[$u][$k] = $array[$u][$k]; } // end loop #2 } // end loop #1 // sort sort($output); // return sorted array return $output; } // end function ?> // +------------------------------------------------------ // And here are the results from the print_r function // +------------------------------------------------------ Array ( [0] => Array ( [password] => drow_ssap [account_type] => user [username] => eman_resu ) [1] => Array ( [password] => pass_word [account_type] => admin [username] => user_name ) [2] => Array ( [password] => yesitdoes [account_type] => admin [username] => php_roxs ) ) P.S. - Sorry for the lengthly un-neat function. But it does work. You are welcome to modify it as you wish. If someone could clean it up and use not so many loops, that would be great. ---- Manual Page -- http://www.php.net/manual/en/function.sort.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39886 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39886&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39886&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65127) next »