How can I sort arrays in php using a custom alphabet?

I want to sort arrays using a key in php. but the alphabet that I use is not the usual English alphabet, but the alphabet itself. Is it possible?

My alphabet

$alphabet = "AjawbpfmnrhHxXsSqkgtTdD ="; 

The array looks like this:

 Array ( [=k_0] => Array( [0] => DI.3,2 &dwA-nTr& @ Hrw@ [1] => mA [2] => =k [3] => Sfj,t [4] => =k [5] => pXr ) [aA_2] => Array( [0] => DI.7,4 &dwA-nTr& @Hrw-smA-tA, wj@ [1] => snD [2] => aA [3] => Sfj,t [4] => jt [5] => jt,w ) [sqA_1] => Array( [0] => DI.6,18 &dwA-nTr& @ nswt@ [1] => ra [2] => sqA [3] => Sfj,t [4] => =s [5] => r ) ); 

so if I sort this array after my alphabet, the array with the key [=k_0] should be at the end.

+4
source share
4 answers

You can use the usort() function and provide your own sorting logic.

See php.net for an example.

Edit : use uksort , not usort . See http://www.php.net/manual/en/function.uksort.php . Thanks @ Darien!

A slightly modified example from php.net - added source code with $alphabet mapping:

 function cmp($a, $b) { // custom sort order - just swapps 2 and 3. $alphabet = array (1 => 1, 2 => 3, 3 => 2, 4 => 4, 5 => 5, 6=> 6); if ($alphabet[$a] == $alphabet[$b]) { return 0; } return ($alphabet[$a] < $alphabet[$b]) ? -1 : 1; } $a = array(3 => 'c' , 2 => 'b', 5 => 'e', 6 => 'f', 1=>'a'); uksort($a, "cmp"); foreach ($a as $key => $value) { echo "$key: $value\n"; } 
+5
source

Given your $alphabet = "AjawbpfmnrhHxXsSqkgtTdD"; and assuming that A < j < A , etc., for your comment, convert each key in the alternative alphabet to a series in the famous alphabet, for example. use type mapping:

  your alphabet: AjawbpfmnrhHxXsSqkgtTdD 'real'alphabet: abcdefghijklmnopqrstuvw 

So, the key is 'Ajaw' => 'abcd' and 'fmnr' => 'ghij' , etc. Then it turns your keys into something you can sort using the normal php functions. You need to somehow handle characters not present in your original alphabet, though.

Something like this might work - you will need two conversion functions (from your alphabet to the "real" alphabet and vice versa), and then a comparator, for example. uksort .

My two cents - thanks for clarifying the original question.

0
source
 <?php $arr = [8,10,12,18,20,7,4,6,2,20,0]; //take array $a= sortasc($arr); // call function function sortasc($arr){ for($i=0;$i<=count($arr);$i++){ for($j=1;$j<=count($arr)-1;$j++){ if($arr[$j-1]>$arr[$j]){ $temp = $arr[$j]; $arr[$j]= $arr[$j-1]; $arr[$j-1] = $temp; } } } return $arr; } ?> 
-1
source

See this code:

 <?php $arr = array('wr' => 1, 'wrS' => 6, 'wr,w' => 3, 'wr.w' => 4, 'wr-qA' => 2, 'wrs' => 5); function compare_by_alphabet(array $alphabet, $str1, $str2) { $l1 = strlen($str1); $l2 = strlen($str2); $c = min($l1, $l2); for ($i = 0; $i < $c; $i++) { $s1 = $str1[$i]; $s2 = $str2[$i]; if ($s1===$s2) continue; $i1 = array_search($s1, $alphabet); if ($i1===false) continue; $i2 = array_search($s2, $alphabet); if ($i2===false) continue; if ($i2===$i1) continue; if ($i1 < $i2) return -1; else return 1; } if ($l1 < $l2) return -1; elseif ($l1 > $l2) return 1; return 0; } function compare_keys_by_alphabet($a, $b) { static $alphabet = array('-', ',', '.', 'A', 'j', 'a', 'w', 'b', 'p', 'f', 'm', 'n', 'r', 'h', 'H', 'x', 'X', 's', 'S', 'q', 'β€Œβ€‹k', 'g', 't', 'T', 'd', 'D', '=', '/', '(', ')', '[', ']', '<', '>', '{', '}', '\'', '*', '#', 'I', 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, '&', '@'); return compare_by_alphabet($alphabet, $a, $b); } uksort($arr, 'compare_keys_by_alphabet'); print_r($arr); 

Result:

 Array ( [wr] => 1 [wr-qA] => 2 [wr,w] => 3 [wr.w] => 4 [wrs] => 5 [wrS] => 6 ) 
-3
source

All Articles