Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UArray.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\utils\base;
4
6
16class UArray extends UArrayAsTrait {
17
24 public static function isAssociative($array) {
25 return (\array_keys ( $array ) !== \range ( 0, \count ( $array ) - 1 ));
26 }
27
35 public static function extractKeys($array, $keys) {
36 $result = [ ];
37 foreach ( $keys as $key ) {
38 if (isset ( $array [$key] )) {
39 $result [$key] = $array [$key];
40 }
41 }
42 return $result;
43 }
44
52 public static function getValue($array, $key, $pos) {
53 if (\array_key_exists ( $key, $array )) {
54 return $array [$key];
55 }
56 $values = \array_values ( $array );
57 if ($pos < \count ( $values ))
58 return $values [$pos];
59 }
60
68 public static function getRecursive($array, $key, $default = null) {
69 if (\array_key_exists ( $key, $array )) {
70 return $array [$key];
71 } else {
72 foreach ( $array as $item ) {
73 if (is_array ( $item )) {
74 return self::getRecursive ( $item, $key, $default );
75 }
76 }
77 }
78 return $default;
79 }
80
81 public static function getDefaultValue($array, $key, $default) {
82 return $array [$key] ?? $default;
83 }
84
92 public static function save($array, $filename) {
93 $content = "<?php\nreturn " . self::asPhpArray ( $array, "array", 1, true ) . ";";
94 return UFileSystem::save ( $filename, $content );
95 }
96
97 public static function remove($array, $search) {
98 if (\is_array ( $search )) {
99 foreach ( $search as $val ) {
100 $array = self::removeOne ( $array, $val );
101 }
102 } else {
103 $array = self::removeOne ( $array, $search );
104 }
105 return \array_values ( $array );
106 }
107
115 public static function removeByKey($array, $key) {
116 if (isset ( $array [$key] )) {
117 unset ( $array [$key] );
118 }
119 return $array;
120 }
121
122 public static function removeRecursive(&$array, $key) {
123 foreach ( $array as &$item ) {
124 if (\array_key_exists ( $key, $item )) {
125 unset ( $item [$key] );
126 } elseif (\is_array ( $item )) {
127 self::removeRecursive ( $item, $key );
128 }
129 }
130 }
131
132 public static function removeByKeys($array, $keys) {
133 $assocKeys = [ ];
134 foreach ( $keys as $key ) {
135 $assocKeys [$key] = true;
136 }
137 return \array_diff_key ( $array, $assocKeys );
138 }
139
140 public static function removeOne($array, $search) {
141 if (($key = \array_search ( $search, $array )) !== false) {
142 unset ( $array [$key] );
143 }
144 return $array;
145 }
146
147 public static function update(&$array, $search, $newValue) {
148 if (($key = \array_search ( $search, $array )) !== false) {
149 $array [$key] = $newValue;
150 }
151 return $array;
152 }
153
159 public static function doubleBackSlashes(&$array) {
160 return \array_walk ( $array, function (&$value) {
161 $value = UString::doubleBackSlashes ( $value );
162 } );
163 }
164
165 public static function iSearch($needle, $haystack, $strict = false) {
166 return \array_search ( strtolower ( $needle ), array_map ( 'strtolower', $haystack ), $strict );
167 }
168
169 public static function iRemove($array, $search) {
170 if (\is_array ( $search )) {
171 foreach ( $search as $val ) {
172 $array = self::iRemoveOne ( $array, $val );
173 }
174 } else {
175 $array = self::iRemoveOne ( $array, $search );
176 }
177 return \array_values ( $array );
178 }
179
180 public static function iRemoveOne($array, $search) {
181 if (($key = self::iSearch ( $search, $array )) !== false) {
182 unset ( $array [$key] );
183 }
184 return $array;
185 }
186
187 public static function implodeAsso($array, $glue, $op = '=', $quoteKey = '"', $quoteValue = '"') {
188 $res = [ ];
189 foreach ( $array as $k => $v ) {
190 if(\is_string($k)){
191 $res [] = "{$quoteKey}{$k}{$quoteKey}{$op}{$quoteValue}{$v}{$quoteValue}";
192 }else{
193 $res [] = "{$quoteKey}{$v}{$quoteKey}";
194 }
195 }
196 return \implode ( $glue, $res );
197 }
198
199 public static function toJSON($array) {
200 $result = [ ];
201 foreach ( $array as $k => $v ) {
202 if (\is_array ( $v )) {
203 $v = self::toJSON ( $v );
204 } elseif ($v != null && UString::startswith ( $v, 'js:' )) {
205 $v = \substr ( $v, 3 );
206 } else {
207 $v = '"' . $v . '"';
208 }
209 $result [] = '"' . $k . '": ' . $v;
210 }
211 return '{' . \implode ( ',', $result ) . '}';
212 }
213}
Ubiquity\utils\base\traits$UArrayAsTrait This class is part of Ubiquity.