Skip to content

Commit 988240a

Browse files
committed
add new method for array repleace
1 parent 01743a3 commit 988240a

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

src/Arr/ArrayHelper.php

+19-19
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static function toIterator($array): Traversable
102102
*
103103
* @return mixed
104104
*/
105-
public static function toObject($array, $class = stdClass::class)
105+
public static function toObject($array, string $class = stdClass::class)
106106
{
107107
$object = new $class;
108108

@@ -387,7 +387,7 @@ public static function crossJoin(...$arrays): array
387387
*
388388
* @return array
389389
*/
390-
public static function divide($array): array
390+
public static function divide(array $array): array
391391
{
392392
return [array_keys($array), array_values($array)];
393393
}
@@ -456,7 +456,7 @@ public static function exists(array $array, $key): bool
456456
*
457457
* @return array
458458
*/
459-
public static function flatten($array, int $depth = INF): array
459+
public static function flatten(array $array, int $depth = INF): array
460460
{
461461
return array_reduce($array, static function ($result, $item) use ($depth) {
462462
if (is_object($item) && method_exists($item, 'toArray')) {
@@ -571,7 +571,7 @@ public static function has($array, $keys): bool
571571
*
572572
* @return array
573573
*/
574-
public static function prepend($array, $value, $key = null): array
574+
public static function prepend(array $array, $value, $key = null): array
575575
{
576576
if (null === $key) {
577577
array_unshift($array, $value);
@@ -585,13 +585,13 @@ public static function prepend($array, $value, $key = null): array
585585
/**
586586
* remove the $key of the $arr, and return value.
587587
*
588-
* @param string $key
588+
* @param string|int $key
589589
* @param array $arr
590590
* @param mixed $default
591591
*
592592
* @return mixed
593593
*/
594-
public static function remove(&$arr, $key, $default = null)
594+
public static function remove(array &$arr, $key, $default = null)
595595
{
596596
if (isset($arr[$key])) {
597597
$value = $arr[$key];
@@ -629,7 +629,7 @@ public static function pull(&$array, $key, $default = null)
629629
*
630630
* @return array
631631
*/
632-
public static function only($array, $keys): array
632+
public static function only(array $array, $keys): array
633633
{
634634
return array_intersect_key($array, array_flip((array)$keys));
635635
}
@@ -641,7 +641,7 @@ public static function only($array, $keys): array
641641
*
642642
* @return array
643643
*/
644-
public static function shuffle($array): array
644+
public static function shuffle(array $array): array
645645
{
646646
shuffle($array);
647647

@@ -656,7 +656,7 @@ public static function shuffle($array): array
656656
*
657657
* @return array
658658
*/
659-
public static function where($array, callable $callback): array
659+
public static function where(array $array, callable $callback): array
660660
{
661661
return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
662662
}
@@ -680,9 +680,9 @@ public static function wrap($value): array
680680
/**
681681
* array 递归 转换成 字符串
682682
*
683-
* @param array $array [大于1200字符 strlen($string)>1200
683+
* @param array $array
684684
* @param int $length
685-
* @param array|int $cycles [至多循环六次 $num >= 6
685+
* @param int $cycles 至多循环六次 $num >= 6
686686
* @param bool $showKey
687687
* @param bool $addMark
688688
* @param string $separator
@@ -692,12 +692,12 @@ public static function wrap($value): array
692692
*/
693693
public static function toString(
694694
$array,
695-
$length = 800,
696-
$cycles = 6,
697-
$showKey = true,
698-
$addMark = false,
699-
$separator = ', ',
700-
$string = ''
695+
int $length = 800,
696+
int $cycles = 6,
697+
bool $showKey = true,
698+
bool $addMark = false,
699+
string $separator = ', ',
700+
string $string = ''
701701
): string {
702702
if (!is_array($array) || empty($array)) {
703703
return '';
@@ -754,9 +754,9 @@ public static function toStringNoKey(
754754
* @param array $array
755755
* @param int $length
756756
*
757-
* @return mixed|null|string|string[]
757+
* @return string
758758
*/
759-
public static function toFormatString($array, $length = 400)
759+
public static function toFormatString($array, int $length = 400): string
760760
{
761761
$string = var_export($array, true);
762762

src/Arr/Traits/ArrayMergeTrait.php

+20
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@
2323
*/
2424
trait ArrayMergeTrait
2525
{
26+
/**
27+
* 替换值合并数组
28+
* - 只会将同时存在于两个数组的key,使用第二个数组对应的值替换
29+
*
30+
* @param array $base
31+
* @param array $replacements
32+
*
33+
* @return array
34+
*/
35+
public static function replace(array $base, array $replacements): array
36+
{
37+
foreach ($base as $key => $value) {
38+
if (isset($replacements[$key])) {
39+
$base[$key] = $replacements[$key];
40+
}
41+
}
42+
43+
return $base;
44+
}
45+
2646
/**
2747
* quick merge `append` array to `base` array.
2848
* - Process at most secondary arrays

src/Arr/Traits/ArrayValueGetSetTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static function set(array &$array, $key, $value): array
130130
*
131131
* @return array
132132
*/
133-
public static function gets(array &$data, array $needKeys = [], $unsetKey = false): array
133+
public static function gets(array &$data, array $needKeys = [], bool $unsetKey = false): array
134134
{
135135
$needed = [];
136136

0 commit comments

Comments
 (0)