Skip to content

Commit 1a903e8

Browse files
committed
added helper arrayUnset
1 parent 4c9261b commit 1a903e8

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/Helper.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,36 @@ public static function arraySet(&$target, $key, $value, $overwrite = true)
158158
return $target;
159159
}
160160

161+
162+
/**
163+
* Unset an item on an array or object using dot notation.
164+
*
165+
* @param mixed $target
166+
* @param string|array $key
167+
* @return mixed
168+
*/
169+
public static function arrayUnset(&$target, $key)
170+
{
171+
if (!is_array($target)) {
172+
return $target;
173+
}
174+
175+
$segments = is_array($key) ? $key : explode('.', $key);
176+
$segment = array_shift($segments);
177+
178+
if ($segment == '*') {
179+
$target = [];
180+
} elseif ($segments) {
181+
if (array_key_exists($segment, $target)) {
182+
static::arrayUnset($target[$segment], $segments);
183+
}
184+
} elseif (array_key_exists($segment, $target)) {
185+
unset($target[$segment]);
186+
}
187+
188+
return $target;
189+
}
190+
161191
/**
162192
* Get snake_case format from given string
163193
*
@@ -171,7 +201,7 @@ public static function snakeCase($value, $delimiter = '_')
171201
$value = preg_replace('/\s+/u', '', ucwords($value));
172202
$value = strtolower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
173203
}
174-
204+
175205
return $value;
176206
}
177207

tests/HelperTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testArrayGet()
4242
$this->assertEquals(Helper::arrayGet($array, 'foo.bar'), $array['foo']['bar']);
4343
$this->assertEquals(Helper::arrayGet($array, 'foo.bar.baz'), $array['foo']['bar']['baz']);
4444
$this->assertEquals(Helper::arrayGet($array, 'one.two.three'), 123);
45-
45+
4646
$this->assertNull(Helper::arrayGet($array, 'foo.bar.baz.qux'));
4747
$this->assertNull(Helper::arrayGet($array, 'one.two'));
4848
}
@@ -89,7 +89,7 @@ public function testArraySet()
8989

9090
Helper::arraySet($array, 'comments.*.id', null, false);
9191
Helper::arraySet($array, 'comments.*.x.y', 1, false);
92-
92+
9393
$this->assertEquals($array, [
9494
'comments' => [
9595
['id' => null, 'text' => 'foo', 'x' => ['y' => 1]],
@@ -99,4 +99,34 @@ public function testArraySet()
9999
]);
100100
}
101101

102+
public function testArrayUnset()
103+
{
104+
$array = [
105+
'users' => [
106+
'one' => 'user_one',
107+
'two' => 'user_two',
108+
],
109+
'stuffs' => [1, 'two', ['three'], null, false, true],
110+
'message' => "lorem ipsum",
111+
];
112+
113+
Helper::arrayUnset($array, 'users.one');
114+
$this->assertEquals($array, [
115+
'users' => [
116+
'two' => 'user_two',
117+
],
118+
'stuffs' => [1, 'two', ['three'], null, false, true],
119+
'message' => "lorem ipsum",
120+
]);
121+
122+
Helper::arrayUnset($array, 'stuffs.*');
123+
$this->assertEquals($array, [
124+
'users' => [
125+
'two' => 'user_two',
126+
],
127+
'stuffs' => [],
128+
'message' => "lorem ipsum",
129+
]);
130+
}
131+
102132
}

0 commit comments

Comments
 (0)