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 | } |