if (!function_exists('array_remove_element_at')) { // FUTURISTIC: MODEL AFTER JAVA Vector.removeElementAt(index)
/**
* Function modeled after java.util.Vector.removeElementAt((integer)index) - remove element at array index and return it
*
* @access public
* @param int $index
* @param array $array (reference)
* @param mixed $key
* @return mixed $element
*/
function array_remove_element_at($index, &$array, $key = '') {
if ((is_numeric($index) && $array[$index]) || (!is_numeric($index) && $array[$key])) {
if (is_numeric($index) && $array[$index]) {
$element = $array[$index];
unset($array[$index]);
} elseif (!is_numeric($index) && $array[$key]) {
$element = $array[$key];
unset($array[$key]);
}
}
return $element;
}
}
My function should, in theory, remove an array element, whether enumerative or associative, and return the removed element.
I wanted to make a crossbreed of array_shift() and java.util.Vector.removeElementAt((int)index) inasmuch as remove an array element.
However, if the array is enumerative, I want to be able to reorder the array so that if you have:
array ('apple', 'banana', 'orange');
And you remove $array[1] you should have:
array (0 => 'apple', 2 => 'orange');
How can I do that? I am not familiar with reordering arrays without looping, and I can't afford to loop because this function is called within several embedded loops as it is!
Thanx
Phil