-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathphpArrayMultiSort.php
41 lines (36 loc) · 1.19 KB
/
phpArrayMultiSort.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
<?php
/*
Purpose : Read a TSV file into an array and sort on multiple fields
Author : Ap.Muthu <[email protected]>
Release : 2020-03-27
Reference: PHP Manual for array_multisort()
*/
$dataarray = array();
$fields = array();
$i = 0;
// data.tsv is a tab separated file with the first row having the field names
// For example here: "LinkID LinkCat DocDate DocType URL ParseDT"
// Read in the TSV file into an array
foreach (file('data.tsv') as $row) {
$row = str_replace("\n", "", $row); // remove ending line feed
$row = str_replace("\r", "", $row); // remove ending carriage return if any
$rec = explode("\t", $row);
if(empty($fields)) {
$fields = $rec;
} else {
foreach($rec as $k => $fld) {
$dataarray[$i][$fields[$k]] = $fld;
}
$i++;
}
}
// get a list of sort columns and their data to pass to array_multisort
$sort = array();
foreach($dataarray as $k=>$v) {
$sort['DocDate'][$k] = $v['DocDate'];
$sort['LinkCat'][$k] = $v['LinkCat'];
$sort['DocType'][$k] = $v['DocType'];
}
// sort by DocDate desc, LinkCat and then DocType
array_multisort($sort['DocDate'], SORT_DESC, $sort['LinkCat'], SORT_ASC, $sort['DocType'], SORT_ASC, $dataarray);
echo print_r($dataarray, true);