-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sortbylastchar.php
37 lines (31 loc) · 1.03 KB
/
Sortbylastchar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
class Sortbylastchar{
/**
* take an array return a string sorted alphabetically by the last character of each word.
*/
public static function sortCharacter($_parametters)
{
// usort : Trie un tableau en utilisant une fonction de comparaison
// strcmp — Comparaison binaire de chaînes
$arrayPamars = explode(' ', $_parametters);
$tmp_reverse = Sortbylastchar::reverseWordsValues($arrayPamars);
echo '<br>';
usort($tmp_reverse, 'strcmp');
$arrangedArray = Sortbylastchar::reverseWordsValues($tmp_reverse);
return ($arrangedArray);
}
/**
* take an array and reverse all words in array
*/
public function reverseWordsValues(array $_parametters)
{
foreach($_parametters as $key => $values){
$tmp_reverse[] = strrev($values);
}
return $tmp_reverse;
}
}
// 'test the function'
$values = "stab traction artist approach";
// $values = "herb camera dynamic";
Sortbylastchar::sortCharacter($values);