-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharrayFunctions.inc.php
More file actions
71 lines (67 loc) · 2.33 KB
/
Copy patharrayFunctions.inc.php
File metadata and controls
71 lines (67 loc) · 2.33 KB
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
<?php
function echo2DArray($array)
{
echo "<p>There are " . count($array) . " rows in the 2D array<br />";
echo "Each record has " . count($array[0]) . " fields</p>";
foreach ($array as $indexR=>$record)
{
echo "$indexR ";
foreach ($record as $indexF=>$field)
echo "$indexF: $field ";
echo "<br />";
}
}
function search_2d_array($array, $key, $value) {
for ($i = 0; $i < count($array); $i++)
{
if (isset($array[$i][$key])) {
if ($array[$i][$key] == $value) {
return $i;
}
}
}
return false;
};
function usortByKey(&$array, $key, $asc=SORT_ASC) {
$sort_flags = array(SORT_ASC, SORT_DESC);
if(!in_array($asc, $sort_flags))
throw new InvalidArgumentException('sort flag only accepts SORT_ASC or SORT_DESC');
$cmp = function(array $a, array $b) use ($key, $asc, $sort_flags) {
if(!isset($a[$key]) || !isset($b[$key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if($a[$key] == $b[$key]) return 0;
return ($asc==SORT_ASC xor $a[$key] < $b[$key]) ? 1 : -1;
};
usort($array, $cmp);
};
function usortByArrayKey(&$array, $key, $asc=SORT_ASC) {
$sort_flags = array(SORT_ASC, SORT_DESC);
if(!in_array($asc, $sort_flags))
throw new InvalidArgumentException('sort flag only accepts SORT_ASC or SORT_DESC');
$cmp = function(array $a, array $b) use ($key, $asc, $sort_flags) {
if(!is_array($key)) { //just one key and sort direction
if(!isset($a[$key]) || !isset($b[$key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if($a[$key] == $b[$key]) return 0;
return ($asc==SORT_ASC xor $a[$key] < $b[$key]) ? 1 : -1;
}
else
{ //using multiple keys for sort and sub-sort
foreach($key as $sub_key => $sub_asc) {
//array can come as 'sort_key'=>SORT_ASC|SORT_DESC or just 'sort_key', so need to detect which
if(!in_array($sub_asc, $sort_flags)) { $sub_key = $sub_asc; $sub_asc = $asc; }
//just like above, except 'continue' in place of return 0
if(!isset($a[$sub_key]) || !isset($b[$sub_key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if($a[$sub_key] == $b[$sub_key]) continue;
return ($sub_asc==SORT_ASC xor $a[$sub_key] < $b[$sub_key]) ? 1 : -1;
}
return 0;
}
};
usort($array, $cmp);
};
?>