forked from biggins/Array-Utility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayUtil.php
131 lines (102 loc) · 2.93 KB
/
ArrayUtil.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
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
<?php
class ArrayUtil
{
/**
* take an array and split into the given number of arrays with equal number of elements
* if an uneven number of elements one (or more) arrays may have more elements then the others
*
* @example http://snippi.com/s/9ls9sug
*
* @param array The array we want to split
* @param int The number of sections we want
* @return array The resulting split array
*/
public static function splitArray($array, $sections)
{
if(count($array) < $sections) {
$chunkSize = 1;
} else {
$chunkSize = (count($array) / $sections);
}
return array_chunk($array, $chunkSize, true);
}
/**
* Add new elements to the given array after the element with the supplied key
*
* @example http://snippi.com/s/6trt9kq
*
* @param array The array we want to add to
* @param string|int The key we wish to add our new elements after.
* @param array The elements we wish to add
* @return array The resulting array with new elements
*/
public static function addAfter($array, $key, $new_elements)
{
$offset = self::getOffsetByKey($array, $key);
if ($offset >= 0) {
// increment cause we want to actually splice in from the element AFTER the one we found
$offset++;
// get the slice, and insert the new elements and rebuild the array
$array_items = array_splice($array, $offset);
$new_elements += $array_items;
$array += $new_elements;
}
return $array;
}
/**
* get the offset of an element within an array based on the key
* useful for associative arrays
*
* @param array The containing array
* @param string The key to search for
* @return int|null The offset within an array | null if not found
*/
public static function getOffsetByKey($array, $needle)
{
$offset = 0;
foreach ($array as $key => $value) {
if ($key === $needle) {
return $offset;
}
$offset++;
}
return null;
}
/**
* get the offset of an element within an array based on the element value
* useful for associative arrays
*
* @param array The containing array
* @param string The value to search for
* @return int|null The offset within an array | null if not found
*/
public static function getOffsetByValue($array, $needle)
{
$offset = 0;
foreach ($array as $key => $value) {
if ($value === $needle) {
return $offset;
}
$offset++;
}
return null;
}
/**
* Move Item
*
* Moves an existing array item to reposition it after another item.
*
* @param array The array we want to do the reordering in
* @param string|int The element key we wish to move
* @param array The element key that'll be before the one we're moving
* @return array The resulting array with reordered elements
*/
public static function moveItem($array, $key, $move_after) {
$move_item = array(
$key => $array[$key]
);
unset($array[$key]);
$result = self::addAfter($array, $move_after, $move_item);
return $result;
}
}