Arrays.php
4.15 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace AC\Helper;
class Arrays {
/**
* @param mixed $array
*
* @return bool
*/
public function is_associative( $array ) {
if ( ! is_array( $array ) ) {
return false;
}
foreach ( $array as $key => $value ) {
if ( is_string( $key ) ) {
return true;
}
}
return false;
}
/**
* @param array $keys
* @param mixed $value
* @param array $result
*
* @return array
*/
public function add_nested_value( array $keys, $value, array $result = [] ) {
$key = array_shift( $keys );
if ( $keys ) {
$value = $this->add_nested_value( $keys, $value, is_array( $result[ $key ] ) ? $result[ $key ] : [] );
}
$result[ $key ] = $value;
return $result;
}
/**
* @param array $array
* @param array $keys
*
* @return mixed
*/
public function get_nested_value( array $array, array $keys ) {
foreach ( $keys as $key ) {
if ( ! isset( $array[ $key ] ) ) {
return null;
}
$array = $array[ $key ];
}
return $array;
}
/**
* Implode for multi dimensional array
*
* @param string $glue
* @param string|array $pieces
*
* @return string Imploded array
* @since 3.0
*/
public function implode_recursive( string $glue, $pieces ): string {
if ( is_scalar( $pieces ) ) {
return $pieces;
}
$scalars = [];
if ( is_array( $pieces ) ) {
foreach ( $pieces as $r_pieces ) {
if ( is_array( $r_pieces ) ) {
$scalars[] = $this->implode_recursive( $glue, $r_pieces );
}
if ( is_scalar( $r_pieces ) ) {
$scalars[] = $r_pieces;
}
}
}
return implode( $glue, array_filter( $scalars, 'strlen' ) );
}
/**
* Indents any object as long as it has a unique id and that of its parent.
*
* @param array $array
* @param int $parentId
* @param string $parentKey
* @param string $selfKey
* @param string $childrenKey
*
* @return array Indented Array
* @since 1.0
*/
public function indent(
$array,
$parentId = 0,
$parentKey = 'post_parent',
$selfKey = 'ID',
$childrenKey = 'children'
) {
$indent = [];
$i = 0;
foreach ( $array as $v ) {
if ( $v->$parentKey == $parentId ) {
$indent[ $i ] = $v;
$indent[ $i ]->$childrenKey = $this->indent( $array, $v->$selfKey, $parentKey, $selfKey );
$i++;
}
}
return $indent;
}
/**
* Remove empty values from array
*
* @param array $array
*
* @return array
*/
public function filter( $array ) {
return array_filter( $array, [ ac_helper()->string, 'is_not_empty' ] );
}
/**
* Insert element into array at specific position
*
* @param array $array
* @param array $insert
* @param string $position
*
* @return array
*/
public function insert( $array, $insert, $position ) {
$new = [];
foreach ( $array as $key => $value ) {
$new[ $key ] = $value;
if ( $key === $position ) {
$new = array_merge( $new, $insert );
}
}
return $new;
}
/**
* Get duplicates from array
*
* @param array $array
*
* @return array
*/
public function get_duplicates( array $array ) {
return array_intersect( $array, array_unique( array_diff_key( $array, array_unique( $array ) ) ) );
}
/**
* Returns all integers from an array or comma separated string
*
* @param array|string $mixed
*
* @return int[]
*/
public function get_integers_from_mixed( $mixed ) {
$string = ac_helper()->array->implode_recursive( ',', $mixed );
return ac_helper()->string->string_to_array_integers( $string );
}
/**
* @param array $array
* @param string $glue
*
* @return string
*/
public function implode_associative( array $array, $glue ) {
_deprecated_function( __METHOD__, '5.7.1' );
return '';
}
/**
* Replace a single key in an associative array
*
* @param array $input Input array.
* @param int|string $old_key Key to replace.
* @param int|string $new_key Key to replace $old_key with
*
* @return array
* @since 2.2.7
*/
public function key_replace( $input, $old_key, $new_key ) {
$keys = array_keys( $input );
$old_key_pos = array_search( $old_key, $keys );
if ( $old_key_pos === false ) {
return $input;
}
$keys[ $old_key_pos ] = $new_key;
return array_combine( $keys, array_values( $input ) );
}
}