note 97461 deleted from function.array-splice by danbrown
| From: | danbrown@php.net | Date: | Wed, 21 Apr 2010 13:21:31 +0000 |
| Subject: | note 97461 deleted from function.array-splice by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168472@lists.php.net to get a copy of this message | ||
Note Submitter: php dot net at webdevelopers dot cz
----
Simple array_ksplice - version of array_splice preserving the keys:
/**
* array_splice does not preserve the replacement keys.
*
* Example:
* $pos=(int) array_search('text/html', array_keys($list[$id]['data']));
* array_ksplice($list[$id]['data'], $pos, 0, array('text/xml' =>
'<b>testX</b>'));
*
* @access public
* @param array &$input The input array
* @param int $offset The sequence will start at that offset in the array.
* @param int $length How many elements will be removed
* @param array $replacement The removed elements are replaced with elements from this array.
* @return void
*/
function array_ksplice(&$input, $offset, $length, $replacement) {
$input=array_merge(
array_slice($input, 0, $offset, true),
$replacement,
array_slice($input, $offset + $length, count($input) - $offset - $length, true),
$replacement // In case that the tail contains the same keys as $replacement do replace
replaced values
);
}