array_splice() for associative arrays
Say I have an associative array:
array(
"color" => "red",
"taste" => "sweet",
"season" => "summer"
);
and I want to introduce a new element into it:
"texture" => "bumpy"
behind the 2nd item but preserving all the array keys:
array(
"color" => "red",
"taste" => "sweet",
"texture" => "bumpy",
"season" => "summer"
);
is there a function to do that? array_splice()
won't cut it, it can work with numeric keys only.
Solution 1:
I think you need to do that manually:
# Insert at offset 2
$offset = 2;
$newArray = array_slice($oldArray, 0, $offset, true) +
array('texture' => 'bumpy') +
array_slice($oldArray, $offset, NULL, true);
Solution 2:
Based on soulmerge's answer I created this handy function:
function array_insert($array,$values,$offset) {
return array_slice($array, 0, $offset, true) + $values + array_slice($array, $offset, NULL, true);
}
Solution 3:
I hate to beat an old issue to death, it seems like some people have come up with some similar answers to mine already. But I'd like to offer a version that I think is just a little more thorough. This function is designed to feel and behave -exactly- like the regular array_splice() one, including its return value and how it handles invalid or negative values. The only difference in that regard is that when defining a replacement array (or string or number) and not a length, you're allowed to use a null value for the length instead of having to pass count($array) as an argument. It will assume that much from a null. 0 is still 0 though.
The only difference in function is of course the $key value parameter, specifying what key to derive a position from to start making changes. The $offset has been left in as well, now used as a modifier for that initial position. Key conflicts will always favor the replacement array but also trigger a warning. And if the key parameter is null or blank, the function will look only to the offset parameter and behave like array_splice, except while maintaining key values. If the key is simply not found though, it will behave the same way array_splice does when given an offset that's beyond the array length; it appends it to the end.
/**
* Remove or replace elements of an associative array by key value.
* @param Object array $input The input associative array
* @param string $key The key whose position in the array determines the start of the removed portion.
* @param int $offset Adjust the start position derived from the key by this value.
* If the sum is positive, it starts from the beginning of the input array. If negative, it starts from the far end.
* @param int $length If length is omitted or null, remove everything from key position to the end of the array.
* If positive, that many elements will be removed.
* If negative, then the end of the removed portion will be that many elements from the end of the array.
* @param mixed $replacement Elements from this array will be inserted at the position of the designated key.
* @return array Returns the array of the extracted elements.
*/
function array_splice_assoc(&$input, $key, $offset = 0, $length = null, $replacement = null)
{
if (!is_array($input)) {
$trace = debug_backtrace();
extract($trace[0]);
trigger_error(
__FUNCTION__."(): expects parameter 1 to be an array, ".gettype($input)." given from $file on line $line",
E_USER_WARNING
);
return false;
}
$offset = (int)$offset;
$replacement = (array)$replacement;
$inputLength = count($input);
if (!is_null($key) && $key !== "") {
$index = array_search($key, $keys = array_keys($input));
if ($index === false) {
$offset = $inputLength;
}
$offset += $index;
}
$index = array_search($key, $keys = array_keys($input));
if ($index === false) {
$offset = $inputLength;
}
$offset += $index;
if ($offset < 0) {
$offset += $inputLength;
if ($offset < 0) {
$offset = 0;
}
}
if (is_null($length)) {
$length = $inputLength;
} elseif ($length < 0) {
$length += $inputLength - $offset;
}
$extracted = array_slice($input, $offset, $length, true);
$start = array_slice($input, 0, $offset, true);
$end = array_slice($input, $offset + $length, $inputLength, true);
$remaining = $start + $end;
if (count($conflict = array_keys(array_intersect_key($remaining, $replacement)))) {
$trace = debug_backtrace();
extract($trace[0]);
trigger_error(
__FUNCTION__."(): key conflict from $file on line $line",
E_USER_WARNING
);
foreach ($conflict as $key) {
if (isset($start[$key])) {
unset($start[$key]);
} else {
unset($end[$key]);
}
}
}
$input = (!empty($replacement)) ? $start + $replacement + $end : $remaining;
return $extracted;
}
So then...
$array1 = array(
"fruit1" => "apple",
"vegetable1" => "carrot",
"vegetable2" => "potato",
"fruit2" => "orange",
"fruit3" => "banana",
"fruit4" => "pear"
);
$array2 = array(
"snack" => "chips",
"vegetable3" => "green bean",
"vegetable1" => "corn"
);
$vegetables = array_splice_assoc($array1, "fruit1", 1, -3);
print_r($array1);
print_r($vegetables);
array_splice_assoc($array2, "vegetable3", -1, 1, $vegetables);
print_r($array2);
/* Output is:
Array
(
[fruit1] => apple
[fruit2] => orange
[fruit3] => banana
[fruit4] => pear
)
Array
(
[vegetable1] => carrot
[vegetable2] => potato
)
PHP Warning: array_splice_assoc(): key conflict from /var/www/php/array_splice_assoc.php on line 97 in /var/www/php/array_splice_assoc.php on line 65
Array
(
[vegetable1] => carrot
[vegetable2] => potato
[vegetable3] => green bean
)
*/
This could also be a simpler way to replace individual array keys while maintaining its position, without having to go through array_values and array_combine.
$array3 = array(
"vegetable1" => "carrot",
"vegetable2" => "potato",
"vegetable3" => "tomato",
"vegetable4" => "green bean",
"vegetable5" => "corn"
);
array_splice_assoc($array3, null, 2, 1, array("fruit1" => $array3['vegetable3']));
print_r($array3);
/* OUTPUT:
Array
(
[vegetable1] => carrot
[vegetable2] => potato
[fruit1] => tomato
[vegetable4] => green bean
[vegetable5] => corn
)
*/
EDIT: I just discovered, apparently array_merge() can't really tell the difference between associative array keys that just happen to be numbers, and regular sequential keys. Merging the arrays using the + operator instead of array_merge() avoids this problem.
Solution 4:
Here's another way:
function array_splice_assoc(&$input, $offset, $length = 0, $replacement = array()) {
$keys = array_keys($input);
$values = array_values($input);
array_splice($keys, $offset, $length, array_keys($replacement));
array_splice($values, $offset, $length, array_values($replacement));
$input = array_combine($keys, $values);
}
Solution 5:
Well, you can rebuild the array from scratch. But the easiest way to go through an associative array in a particular order is to keep a separate ordering array. Like so:
$order=array('color','taste','texture','season');
foreach($order as $key) {
echo $unordered[$key];
}