-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextraFuncs.php
More file actions
138 lines (125 loc) · 4.02 KB
/
Copy pathextraFuncs.php
File metadata and controls
138 lines (125 loc) · 4.02 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
// http://www.php.net/manual/en/function.array-search.php#100057
/**
* Gets the parent stack of a string array element if it is found within the
* parent array
*
* This will not search objects within an array, though I suspect you could
* tweak it easily enough to do that
*
* @param string $child The string array element to search for
* @param array $stack The stack to search within for the child
* @return array An array containing the parent stack for the child if found,
* false otherwise
*/
function getParentStack($child, $stack) {
foreach ($stack as $k => $v) {
if (is_array($v)) {
// If the current element of the array is an array, recurse it and capture the return
$return = getParentStack($child, $v);
// If the return is an array, stack it and return it
if (is_array($return)) {
return array($k => $return);
}
} else {
// Since we are not on an array, compare directly
if ($v == $child) {
// And if we match, stack it and return it
return array($k => $child);
}
}
}
// Return false since there was nothing found
return false;
}
/**
* Gets the complete parent stack of a string array element if it is found
* within the parent array
*
* This will not search objects within an array, though I suspect you could
* tweak it easily enough to do that
*
* @param string $child The string array element to search for
* @param array $stack The stack to search within for the child
* @return array An array containing the parent stack for the child if found,
* false otherwise
*/
function getParentStackComplete($child, $stack) {
$return = array();
foreach ($stack as $k => $v) {
if (is_array($v)) {
// If the current element of the array is an array, recurse it
// and capture the return stack
$stack = getParentStackComplete($child, $v);
// If the return stack is an array, add it to the return
if (is_array($stack) && !empty($stack)) {
$return[$k] = $stack;
}
} else {
// Since we are not on an array, compare directly
if ($v == $child) {
// And if we match, stack it and return it
$return[$k] = $child;
}
}
}
// Return the stack
return empty($return) ? false: $return;
}
// http://www.php.net/manual/en/function.array-search.php#94598
function getKeyPositionInArray($haystack, $keyNeedle)
{
$i = 0;
foreach($haystack as $key => $value)
{
if($key == $keyNeedle)
{
return $i;
}
$i++;
}
}
function getPlayerId($haystack, $needle) {
$i = 0;
foreach($haystack as $playerarray) {
if($playerarray["name"] == $needle) {
return $i;
}
$i++;
}
}
// http://www.php.net/manual/en/function.unset.php#89881
# remove by key:
function array_remove_key ()
{
$args = func_get_args();
return array_diff_key($args[0],array_flip(array_slice($args,1)));
}
# remove by value:
function array_remove_value ()
{
$args = func_get_args();
return array_diff($args[0],array_slice($args,1));
}
// http://www.php.net/manual/en/function.unset.php#74170
/**
* array array_remove ( array input, mixed search_value [, bool strict] )
**/
function array_remove(array &$a_Input, $m_SearchValue, $b_Strict = False) {
$a_Keys = array_keys($a_Input, $m_SearchValue, $b_Strict);
foreach($a_Keys as $s_Key) {
unset($a_Input[$s_Key]);
}
return $a_Input;
}
// somewhere from the php.net page on memory_get_usage() i think? not sure
function echo_memory_usage() {
$mem_usage = memory_get_usage(true);
if ($mem_usage < 1024)
return $mem_usage." bytes";
elseif ($mem_usage < 1048576)
return round($mem_usage/1024,2)." kilobytes";
else
return round($mem_usage/1048576,2)." megabytes";
}
?>