-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortjson.php
87 lines (68 loc) · 2.12 KB
/
sortjson.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
if(!isset($_SERVER['HTTP_REFERER'])){
// redirect them to your desired location
header('location: index.php');
exit;
}
// read json file into array of strings
$jsonstring = file_get_contents("galleryinfo.json");
// save the json data as a PHP array
$phparray = json_decode($jsonstring, true);
// use GET to determine type of sort
// fname, lname, date
if (isset($_GET["type"])){
$type = $_GET["type"];
} else {
$type = "none";
}
$returnData = $phparray;
// Function to sort UID
function array_sort($array, $on, $order=SORT_ASC) {
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
} // array_sort
return $new_array;
}
if ($type == "fname" || $type == "lname") {
# Sort array - The flags SORT_NATURAL & SORT_FLAG_CASE are required to make the
# sorting case insensitive.
foreach ($phparray as $key => $row) {
$sort_by[$key] = $row[$type];
}
array_multisort($sort_by, SORT_ASC, SORT_NATURAL|SORT_FLAG_CASE, $phparray);
} else {
array_sort($phparray, 'UID', SORT_ASC); // Sort by oldest first
}
// update returnData
// copy information into a new array with new index assigned
$returnData = [];
foreach($phparray as $entry) {
$returnData[] = $entry;
} // foreach
// encode the php array to json
$jsoncode = json_encode($returnData, JSON_PRETTY_PRINT);
echo($jsoncode);
?>