Saving array_splice keys

I came across a situation that splicing arrays with stored keys, so I made the following function.
I have reached a solution that wraps each element with an array, but there seem to be some inefficient operators.
Do you have any ideas? Thanks.


array_splice_pk

This saves keys other than array_splice .

Overview:

  • &$input โ†’ same as array_splice .
  • $key โ†’ target key.
  • $use_key_as_offset use the $key parameter as a numeric offset .
  • $length โ†’ same as array_splice .
  • $replacement โ†’ same as array_splice one. But you can also provide a key for each value.

Code:

 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; } 

Example:

 $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); 

Result:

 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" } 
+6
source share
2 answers

I found this in the guide for 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')); ?> 

It is more space and time efficient while keeping keys.

+3
source

Here is a function that stores the keys of the replacement array and has the same argument list as the original array_splice function:

 <?php function array_splice_preserve_keys(&$input, $offset, $length=null, $replacement=array()) { if (empty($replacement)) { return array_splice($input, $offset, $length); } $part_before = array_slice($input, 0, $offset, $preserve_keys=true); $part_removed = array_slice($input, $offset, $length, $preserve_keys=true); $part_after = array_slice($input, $offset+$length, null, $preserve_keys=true); $input = $part_before + $replacement + $part_after; return $part_removed; } // use as normal array_splice_preserve_keys($input, $offset, $length, $replacement); 

See the publication at https://github.com/lode/gaps .

+3
source

Source: https://habr.com/ru/post/945148/