1
2<?php
3$fruits = array("lemon", "orange", "banana", "apple");
4rsort($fruits);
5foreach ($fruits as $key => $val) {
6 echo "$key = $val\n";
7}
8?>
9
10
1
2<?php
3
4$fruits = array("lemon", "orange", "banana", "apple");
5sort($fruits);
6foreach ($fruits as $key => $val) {
7 echo $val;
8}
9/*
10OUTPUT:
11apple
12banana
13lemon
14orange
15*/
16?>
17
18
1function aasort (&$array, $key) {
2 $sorter=array();
3 $ret=array();
4 reset($array);
5 foreach ($array as $ii => $va) {
6 $sorter[$ii]=$va[$key];
7 }
8 asort($sorter);
9 foreach ($sorter as $ii => $va) {
10 $ret[$ii]=$array[$ii];
11 }
12 $array=$ret;
13}
14
15aasort($your_array,"order");
16
1// Fonction de comparaison
2function cmp($a, $b) {
3 if ($a == $b) {
4 return 0;
5 }
6 return ($a < $b) ? -1 : 1;
7}
8
1// array sort php
2$room_details = array(
3 "2020-09-27": [
4 {
5 "content": "how are you",
6 "detail_id": "1",
7 "time": "17:57:28",
8 "chat_time": "2020-09-24 17:57:28",
9 "width": "0",
10 "height": "0",
11 "type": "1",
12 "distance_time": "26 days ago",
13 "avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
14 "position": 2
15 },
16 {
17 "content": "I am fine, thanks",
18 "detail_id": "2",
19 "time": "17:57:45",
20 "chat_time": "2020-09-24 17:57:45",
21 "width": "0",
22 "height": "0",
23 "type": "1",
24 "distance_time": "26 days ago",
25 "avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
26 "position": 2
27 },
28 ],
29 "2020-09-24": [
30 {
31 "content": "how are you",
32 "detail_id": "1",
33 "time": "17:57:28",
34 "chat_time": "2020-09-24 17:57:28",
35 "width": "0",
36 "height": "0",
37 "type": "1",
38 "distance_time": "26 days ago",
39 "avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
40 "position": 2
41 },
42 {
43 "content": "I am fine, thanks",
44 "detail_id": "2",
45 "time": "17:57:45",
46 "chat_time": "2020-09-24 17:57:45",
47 "width": "0",
48 "height": "0",
49 "type": "1",
50 "distance_time": "26 days ago",
51 "avatar": "uploads/MemberImage/20200922-1436-image-5f699b536f438-0.png",
52 "position": 2
53 },
54 ],
55);
56
57sort($room_details);
58
59// result
60// array sort php
61$room_details = array(
62 "2020-09-24": [
63 ...
64 ],
65 "2020-09-27": [
66 ...
67 ],
68);
69