note 55820 added to function.array-keys
| From: | ru dot dy at gmx dot net | Date: | Sun, 14 Aug 2005 16:20:02 +0000 |
| Subject: | note 55820 added to function.array-keys | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93642@lists.php.net to get a copy of this message | ||
I was looking for a function that simply unset a variable amout of values from a one-dimensional
array by key. I ended up with this (returns the array itself if no further parameter than the array
is given, false with no params - does not change the source array)
usage: array_remove(array $input [, mixed key ...])
<?php
function array_remove() {
if ($stack = func_get_args()) {
$input = array_shift($stack);
foreach ($stack as $key) {
unset($input[$key]);
}
return $input;
}
return false;
}
?>
Test:
<?php
$a = array('a'=>'fun', 'b'=>3.14, 'sub'=>
array('1', '2', '3'), 'd'=>'what',
'e' => 'xample', 5 => 'x');
print_r($a);
print_r(array_remove($a, 'd', 'b', 5, 'sub'));
?>
Output:
Array
(
[a] => fun
[b] => 3.14
[sub] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[d] => what
[e] => xample
[5] => x
)
Array
(
[a] => fun
[e] => xample
)
Hope this helps someone.
----
Manual Page -- http://www.php.net/manual/en/function.array-keys.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55820
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55820&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55820&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55820&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55820&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55820&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55820&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55820&report=yes
Search -- http://master.php.net/manage/user-notes.php