-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasort.php
More file actions
43 lines (31 loc) · 717 Bytes
/
asort.php
File metadata and controls
43 lines (31 loc) · 717 Bytes
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
38
39
40
41
42
43
<?php
$arr = ['one' => 'apple', 'app' => 'mao', 3 => 'hello', 'world' => 2, 1 => 'hello'];
function my_asort(array $arr)
{
$vals = array_values($arr);
sort($vals); //sort($keys);
$result = [];
foreach($vals as $v){
$k = array_search($v, $arr);
$result[$k] = $v;
unset($arr[$k]);
}
$keys = array_keys($result);
for ($i = 0; $i < count($keys)-1; $i++)
{
for($j = 0; $j< count($keys)-1; $j++)
{
if($result[$keys[$j]] < $result[$keys[$j+1]])
continue;
if($keys[$j]>$keys[$j+1])
{
$temp = $keys[$j+1];
$keys[$j+1] = $keys[$j];
$keys[$j] = $temp;
}
}
}
return array_combine($keys, $vals);
}
var_dump(my_asort($arr));
?>