Skip to content

Commit 397a029

Browse files
committed
up: update some array helper methods
1 parent 6b825e7 commit 397a029

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/Arr.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
*/
2020
class Arr extends ArrayHelper
2121
{
22+
2223
}

src/Arr/ArrayHelper.php

+36-4
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ public static function toIterator(mixed $array): Traversable
112112
/**
113113
* array data to object
114114
*
115-
* @param Traversable|array $array
116-
* @param string $class
115+
* @param iterable $array
116+
* @param string $class
117117
*
118118
* @return mixed
119119
*/
120-
public static function toObject(Traversable|array $array, string $class = stdClass::class): mixed
120+
public static function toObject(iterable $array, string $class = stdClass::class): mixed
121121
{
122122
$object = new $class;
123123

@@ -408,6 +408,24 @@ public static function collapse(array $array): array
408408
return array_merge(...$results);
409409
}
410410

411+
/**
412+
* like implode() but support any type
413+
*
414+
* @param array $list
415+
* @param string $sep
416+
*
417+
* @return string
418+
*/
419+
public static function join(array $list, string $sep = ','): string
420+
{
421+
$strings = [];
422+
foreach ($list as $value) {
423+
$strings[] = DataHelper::toString($value, true);
424+
}
425+
426+
return implode($sep, $strings);
427+
}
428+
411429
/**
412430
* Cross join the given arrays, returning all possible permutations.
413431
*
@@ -648,6 +666,20 @@ public static function remove(array &$arr, int|string $key, mixed $default = nul
648666
return $value;
649667
}
650668

669+
/**
670+
* @param array $arr
671+
* @param ...$keys
672+
*
673+
* @return array
674+
*/
675+
public static function deleteKeys(array $arr, ...$keys): array
676+
{
677+
foreach ($keys as $key) {
678+
unset($arr[$key]);
679+
}
680+
return $arr;
681+
}
682+
651683
/**
652684
* Get a value from the array, and remove it.
653685
*
@@ -862,7 +894,7 @@ public static function toKVString(array $data, string $kvSep = '=', string $line
862894
{
863895
$lines = [];
864896
foreach ($data as $key => $val) {
865-
$lines = $key . $kvSep . DataHelper::toString($val);
897+
$lines = $key . $kvSep . DataHelper::toString($val, true);
866898
}
867899

868900
return implode($lineSep, $lines);

0 commit comments

Comments
 (0)