mpy*_*pyw 10 php arrays array-splice preserve
我遇到了使用保留键拼接数组的情况,所以我做了以下功能.
我达到了用数组包装每个项目的解决方案,但似乎有一些内存效率低下的语句.
你有什么想法吗?
谢谢.
array_splice_pk这保留了密钥,不同于array_splice.
&$input- >与array_splice一个相同.$key - >目标键.$use_key_as_offset- >使用$key参数作为数字偏移量.$length- >与array_splice一个相同.$replacement- >与array_splice一个相同.但您也可以为每个值提供密钥.function array_splice_pk(&$input, $key, $use_key_as_offset = false, $length = 0, $replacement = null) {
if (!is_array($input) || !is_scalar($key)) {
return array();
}
if ($replacement !== null) {
$replacement = array($replacement);
if (!is_array($replacement[0])) {
$replacement = array($replacement);
}
}
$wrapper = array();
foreach ($input as $k => $v) {
$wrapper[] = array($k => $v);
}
$del_key = null;
foreach ($wrapper as $k => $v) {
if ($use_key_as_offset) {
if ($k === (int)$key) {
$del_key = $k;
break;
}
} else {
if (key($v) == $key) {
$del_key = $k;
break;
}
}
}
if ($del_key === null) {
return array();
}
if ($replacement === null) {
$wrapper_ret = array_splice($wrapper, $del_key, $length);
} else {
$wrapper_ret = array_splice($wrapper, $del_key, $length , $replacement);
}
$ret = $input = array();
foreach ($wrapper_ret as $wrap) {
list($k, $v) = each($wrap);
$ret[$k] = $v;
}
foreach ($wrapper as $wrap) {
list($k ,$v) = each($wrap);
$input[$k] = $v;
}
return $ret;
}
Run Code Online (Sandbox Code Playgroud)
$arr1 = $arr2 = array(
'one' => 'test',
'two' => 'test',
'three' => 'test',
'four' => 'test',
);
$ret1 = array_splice_pk($arr1, 'three', false, 1, array('fizz' => '!!!'));
$ret2 = array_splice_pk($arr2, 2 , true , 1, array('fizz' => '!!!'));
var_dump('Result1', $arr1, $ret1, 'Result2', $arr2, $ret2);
Run Code Online (Sandbox Code Playgroud)
string(7) "Result1"
array(4) {
["one"]=>
string(4) "test"
["two"]=>
string(4) "test"
["fizz"]=>
string(3) "!!!"
["four"]=>
string(4) "test"
}
array(1) {
["three"]=>
string(4) "test"
}
string(7) "Result2"
array(4) {
["one"]=>
string(4) "test"
["two"]=>
string(4) "test"
["fizz"]=>
string(3) "!!!"
["four"]=>
string(4) "test"
}
array(1) {
["three"]=>
string(4) "test"
}
Run Code Online (Sandbox Code Playgroud)
我在array_slice的手册中找到了这个.
<?php
function array_splice_assoc(&$input, $offset, $length, $replacement = array()) {
$replacement = (array) $replacement;
$key_indices = array_flip(array_keys($input));
if (isset($input[$offset]) && is_string($offset)) {
$offset = $key_indices[$offset];
}
if (isset($input[$length]) && is_string($length)) {
$length = $key_indices[$length] - $offset;
}
$input = array_slice($input, 0, $offset, TRUE)
+ $replacement
+ array_slice($input, $offset + $length, NULL, TRUE);
}
$fruit = array(
'orange' => 'orange',
'lemon' => 'yellow',
'lime' => 'green',
'grape' => 'purple',
'cherry' => 'red',
);
// Replace lemon and lime with apple
array_splice_assoc($fruit, 'lemon', 'grape', array('apple' => 'red'));
// Replace cherry with strawberry
array_splice_assoc($fruit, 'cherry', 1, array('strawberry' => 'red'));
?>
Run Code Online (Sandbox Code Playgroud)
在保留密钥的同时,它似乎更节省空间和时间.
我将在 2022 年用我的 PHP 8 知识发布自我回答。
它正确接受负长度/偏移量和字符串偏移量。
function array_splice_assoc(array &$input, int|string $key, ?int $length = null, $replacement = [], bool $use_int_key_as_offset = true): array
{
// Normalize key/offset
$offset = match (true) {
is_string($key) || !$use_int_key_as_offset => array_flip(array_keys($input))[$key] ?? throw new OutOfBoundsException(),
$key < 0 => count($input) + $key,
default => $key,
};
// Normalize length
$length = match (true) {
$length === null => count($input) - $offset,
$length < 0 => count($input) + $length - $offset,
default => $length,
};
// Manipulate each part
$before = array_slice($input, 0, $offset, true);
$removed = array_slice($input, $offset, $length, true);
$after = array_slice($input, $offset + $length, null, true);
// Merge parts, allowing the latter overrides the former
$input = array_replace($before, (array)$replacement, $after);
return $removed;
}
Run Code Online (Sandbox Code Playgroud)
例子:
$array = ['a' => 'A', 'b' => 'B', 3 => 'C', 4 => 'D'];
$original = $array;
$removed = array_splice_assoc($original, 1, 1, [5 => 'E']);
echo json_encode(compact('original', 'removed')) . PHP_EOL;
/*
{"original":{"a":"A","5":"E","3":"C","4":"D"},"removed":{"b":"B"}}
*/
$original = $array;
$removed = array_splice_assoc($original, 2, replacement: [5 => 'E']);
echo json_encode(compact('original', 'removed')) . PHP_EOL;
/*
{"original":{"a":"A","b":"B","5":"E"},"removed":{"3":"C","4":"D"}}
*/
$original = $array;
$removed = array_splice_assoc($original, -3, 1, [5 => 'E']);
echo json_encode(compact('original', 'removed')) . PHP_EOL;
/*
{"original":{"a":"A","5":"E","3":"C","4":"D"},"removed":{"b":"B"}}
*/
$original = $array;
$removed = array_splice_assoc($original, -3, -1, [5 => 'E']);
echo json_encode(compact('original', 'removed')) . PHP_EOL;
/*
{"original":{"a":"A","5":"E","4":"D"},"removed":{"b":"B","3":"C"}}
*/
$original = $array;
$removed = array_splice_assoc($original, 'b', 2, [5 => 'E']);
echo json_encode(compact('original', 'removed')) . PHP_EOL;
/*
{"original":{"a":"A","5":"E","4":"D"},"removed":{"b":"B","3":"C"}}
*/
$original = $array;
$removed = array_splice_assoc($original, 3, 1, [5 => 'E']);
echo json_encode(compact('original', 'removed')) . PHP_EOL;
/*
{"original":{"a":"A","b":"B","3":"C","5":"E"},"removed":{"4":"D"}}
*/
$original = $array;
$removed = array_splice_assoc($original, 3, 1, [5 => 'E'], false);
echo json_encode(compact('original', 'removed')) . PHP_EOL;
/*
{"original":{"a":"A","b":"B","5":"E","4":"D"},"removed":{"3":"C"}}
*/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7316 次 |
| 最近记录: |