16class UArray
extends UArrayAsTrait {
24 public static function isAssociative($array) {
25 return (\array_keys ( $array ) !== \range ( 0, \count ( $array ) - 1 ));
35 public static function extractKeys($array, $keys) {
37 foreach ( $keys as $key ) {
38 if (isset ( $array [$key] )) {
39 $result [$key] = $array [$key];
52 public static function getValue($array, $key, $pos) {
53 if (\array_key_exists ( $key, $array )) {
56 $values = \array_values ( $array );
57 if ($pos < \count ( $values ))
58 return $values [$pos];
68 public static function getRecursive($array, $key, $default =
null) {
69 if (\array_key_exists ( $key, $array )) {
72 foreach ( $array as $item ) {
73 if (is_array ( $item )) {
74 return self::getRecursive ( $item, $key, $default );
81 public static function getDefaultValue($array, $key, $default) {
82 return $array [$key] ?? $default;
92 public static function save($array, $filename) {
93 $content =
"<?php\nreturn " . self::asPhpArray ( $array,
"array", 1,
true ) .
";";
94 return UFileSystem::save ( $filename, $content );
97 public static function remove($array, $search) {
98 if (\is_array ( $search )) {
99 foreach ( $search as $val ) {
100 $array = self::removeOne ( $array, $val );
103 $array = self::removeOne ( $array, $search );
105 return \array_values ( $array );
115 public static function removeByKey($array, $key) {
116 if (isset ( $array [$key] )) {
117 unset ( $array [$key] );
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 );
132 public static function removeByKeys($array, $keys) {
134 foreach ( $keys as $key ) {
135 $assocKeys [$key] =
true;
137 return \array_diff_key ( $array, $assocKeys );
140 public static function removeOne($array, $search) {
141 if (($key = \array_search ( $search, $array )) !==
false) {
142 unset ( $array [$key] );
147 public static function update(&$array, $search, $newValue) {
148 if (($key = \array_search ( $search, $array )) !==
false) {
149 $array [$key] = $newValue;
159 public static function doubleBackSlashes(&$array) {
160 return \array_walk ( $array,
function (&$value) {
161 $value = UString::doubleBackSlashes ( $value );
165 public static function iSearch($needle, $haystack, $strict =
false) {
166 return \array_search ( strtolower ( $needle ), array_map (
'strtolower', $haystack ), $strict );
169 public static function iRemove($array, $search) {
170 if (\is_array ( $search )) {
171 foreach ( $search as $val ) {
172 $array = self::iRemoveOne ( $array, $val );
175 $array = self::iRemoveOne ( $array, $search );
177 return \array_values ( $array );
180 public static function iRemoveOne($array, $search) {
181 if (($key = self::iSearch ( $search, $array )) !==
false) {
182 unset ( $array [$key] );
187 public static function implodeAsso($array, $glue, $op =
'=', $quoteKey =
'"', $quoteValue =
'"') {
189 foreach ( $array as $k => $v ) {
191 $res [] =
"{$quoteKey}{$k}{$quoteKey}{$op}{$quoteValue}{$v}{$quoteValue}";
193 $res [] =
"{$quoteKey}{$v}{$quoteKey}";
196 return \implode ( $glue, $res );
199 public static function toJSON($array) {
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 );
209 $result [] =
'"' . $k .
'": ' . $v;
211 return '{' . \implode (
',', $result ) .
'}';
Ubiquity\utils\base\traits$UArrayAsTrait This class is part of Ubiquity.