forked from Islandora/php_lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.inc
112 lines (104 loc) · 2.15 KB
/
Array.inc
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
<?php
/**
* @file
*
*/
/**
* Recusively copy's an array cloning any references. Perserves Keys, Objects shallow copy unless they implement __clone.
*/
function array_copy_recursive(array &$array) {
$output = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$output[$key] = array_copy_recursive($value);
}
if (is_object($value)) {
$output[$key] = clone $value;
}
else { // Default type copyed by default.
$output[$key] = $value;
}
}
return $output;
}
/**
*
* @param array $array
* @param type $key
* @param type $value
*/
function array_add(array &$array, $key, $value) {
if (isset($key)) {
$array[$key] = $value;
}
else {
$array[] = $value;
}
}
/**
* Given an array of index's this functions attempts a depth first traversal of the given array
* using the provided indices.
*
* @param array $form
* @param array $indices
* @return mixed
* A reference to the value at the final index if found, FALSE otherwise.
*/
function &array_traverse_path(array &$array, array $indices) {
$point = &$array;
foreach ($indices as $index) {
if (empty($point[$index])) {
return FALSE;
}
$point = &$point[$index];
}
return $point;
}
/**
*
* @param array $array
* @param string $type
* @return array
*/
function array_filter_type(array &$array, $type) {
$filtered = array();
foreach ($array as $item) {
if ($item instanceof $type) {
$filtered[] = $item;
}
}
return $filtered;
}
/**
*
* @param array $array
*/
function is_non_empty_array($array) {
return is_array($array) && count($array) > 0;
}
/**
*
* @param type $object
* @return type
*/
function object_as_array($object) {
$array = array();
if (is_object($object) || is_array($object)) {
foreach ($object as $key => $value) {
$array[$key] = is_object($value) ? object_as_array($value) : $value;
}
return $array;
}
return $object;
}
/**
* Returns the first element without modifying the array.
*
* @param array $array
*/
function array_peek(array $array) {
reset($array);
list($key, $value) = each($array);
reset($array);
return array($key, $value);
}