PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.4.1
Admin Columns v4.4.1
7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Helper / Arrays.php
codepress-admin-columns / classes / Helper Last commit date
Select 4 years ago Arrays.php 4 years ago Date.php 4 years ago File.php 4 years ago Html.php 4 years ago Icon.php 4 years ago Image.php 4 years ago Media.php 4 years ago Menu.php 4 years ago Network.php 4 years ago Post.php 4 years ago Strings.php 4 years ago Taxonomy.php 4 years ago User.php 4 years ago
Arrays.php
194 lines
1 <?php
2
3 namespace AC\Helper;
4
5 class Arrays {
6
7 /**
8 * @param mixed $array
9 *
10 * @return bool
11 */
12 public function is_associative( $array ) {
13 if ( ! is_array( $array ) ) {
14 return false;
15 }
16
17 foreach ( $array as $key => $value ) {
18 if ( is_string( $key ) ) {
19 return true;
20 }
21 }
22
23 return false;
24 }
25
26 /**
27 * @param array $array
28 * @param string $glue
29 *
30 * @return string
31 */
32 public function implode_associative( array $array, $glue ) {
33 $result = [];
34
35 foreach ( $array as $key => $item ) {
36 if ( is_array( $item ) ) {
37 $result[] = sprintf( '%s[ %s ]', $key, $this->implode_associative( $item, $glue ) );
38 } else if ( is_numeric( $key ) ) {
39 $result[] = $this->wrap_boolean_in_italic( $item );
40 } else {
41 $result[] = sprintf( '%s: %s', $key, $this->wrap_boolean_in_italic( $item ) );
42 }
43 }
44
45 return implode( $glue, $result );
46 }
47
48 private function wrap_boolean_in_italic( $value ) {
49 if ( is_bool( $value ) ) {
50 $value = sprintf( '<em>%s</em>', $value ? 'true' : 'false' );
51 }
52
53 return $value;
54 }
55
56 /**
57 * Implode for multi dimensional array
58 *
59 * @param string $glue
60 * @param string|array $pieces
61 *
62 * @return string Imploded array
63 * @since 3.0
64 */
65 public function implode_recursive( $glue, $pieces ) {
66 if ( is_array( $pieces ) ) {
67 foreach ( $pieces as $r_pieces ) {
68 if ( is_array( $r_pieces ) ) {
69 $retVal[] = $this->implode_recursive( $glue, $r_pieces );
70 } else {
71 $retVal[] = $r_pieces;
72 }
73 }
74 if ( isset( $retVal ) && is_array( $retVal ) ) {
75 return implode( $glue, $retVal );
76 }
77 }
78
79 if ( is_scalar( $pieces ) ) {
80 return $pieces;
81 }
82
83 return false;
84 }
85
86 /**
87 * Replace a single key in an associative array
88 *
89 * @param array $input Input array.
90 * @param int|string $old_key Key to replace.
91 * @param int|string $new_key Key to replace $old_key with
92 *
93 * @return array
94 * @since 2.2.7
95 */
96 public function key_replace( $input, $old_key, $new_key ) {
97 $keys = array_keys( $input );
98 $old_key_pos = array_search( $old_key, $keys );
99
100 if ( $old_key_pos === false ) {
101 return $input;
102 }
103
104 $keys[ $old_key_pos ] = $new_key;
105
106 return array_combine( $keys, array_values( $input ) );
107 }
108
109 /**
110 * Indents any object as long as it has a unique id and that of its parent.
111 *
112 * @param array $array
113 * @param int $parentId
114 * @param string $parentKey
115 * @param string $selfKey
116 * @param string $childrenKey
117 *
118 * @return array Indented Array
119 * @since 1.0
120 */
121 public function indent( $array, $parentId = 0, $parentKey = 'post_parent', $selfKey = 'ID', $childrenKey = 'children' ) {
122 $indent = [];
123
124 $i = 0;
125 foreach ( $array as $v ) {
126 if ( $v->$parentKey == $parentId ) {
127 $indent[ $i ] = $v;
128 $indent[ $i ]->$childrenKey = $this->indent( $array, $v->$selfKey, $parentKey, $selfKey );
129
130 $i++;
131 }
132 }
133
134 return $indent;
135 }
136
137 /**
138 * Remove empty values from array
139 *
140 * @param array $array
141 *
142 * @return array
143 */
144 public function filter( $array ) {
145 return array_filter( $array, [ ac_helper()->string, 'is_not_empty' ] );
146 }
147
148 /**
149 * Insert element into array at specific position
150 *
151 * @param array $array
152 * @param array $insert
153 * @param string $position
154 *
155 * @return array
156 */
157 public function insert( $array, $insert, $position ) {
158 $new = [];
159 foreach ( $array as $key => $value ) {
160 $new[ $key ] = $value;
161 if ( $key === $position ) {
162 $new = array_merge( $new, $insert );
163 }
164
165 }
166
167 return $new;
168 }
169
170 /**
171 * Get duplicates from array
172 *
173 * @param array $array
174 *
175 * @return array
176 */
177 public function get_duplicates( array $array ) {
178 return array_intersect( $array, array_unique( array_diff_key( $array, array_unique( $array ) ) ) );
179 }
180
181 /**
182 * Returns all integers from an array or comma separated string
183 *
184 * @param array|string $mixed
185 *
186 * @return int[]
187 */
188 public function get_integers_from_mixed( $mixed ) {
189 $string = ac_helper()->array->implode_recursive( ',', $mixed );
190
191 return ac_helper()->string->string_to_array_integers( $string );
192 }
193
194 }