views:

188

answers:

4

I have an array which may contain numeric or associative keys, or both:

$x = array('a', 'b', 'c', 'foo' => 'bar', 'd', 'e');
print_r($x);
/*(
    [0] => a
    [1] => b
    [2] => c
    [foo] => bar
    [3] => d
    [4] => e
)*/

I want to be able to remove an item from the array, renumbering the non-associative keys to keep them sequential:

$x = remove($x, "c");
print_r($x);
/* desired output:
(
    [0] => a
    [1] => b
    [foo] => bar
    [2] => d
    [3] => e
)*/

Finding the right element to remove is no issue, it's the keys that are the problem. unset doesn't renumber the keys, and array_splice works on an offset, rather than a key (ie: take $x from the first example, array_splice($x, 3, 1) would remove the "bar" element rather than the "d" element).

A: 

I've come up with this - though I'm not sure if it's the best:

// given: $arr is the array
//        $item is the item to remove

$key = array_search($item, $arr);  // the key we need to remove
$arrKeys = array_keys($arr);
$keyPos = array_search($key, $arrKeys); // the offset of the item in the array

unset($arr[$key]);
array_splice($arrKeys, $keyPos, 1);

for ($i = $keyPos; $i < count($arrKeys); ++$i) {
    if (is_int($arrKeys[$i])) --$arrKeys[$i]; // shift numeric keys back one
}
$arr = array_combine($arrKeys, $arr);  // recombine the keys and values.

There's a few things I've left out, just for the sake of brevity. For example, you'd check if the array is associative, and also if the key you're removing is a string or not before using the above code.

nickf
A: 

Try array_diff() it may not order the new array correctly though if not the following should work

You will need to iterate over it in the remove function.

function remove($x,$r){
 $c = 0;
 $a = array();
 foreach ($x as $k=>$v){
   if ($v != $r) {
     if (is_int($k)) {
       $a[$c] = $v;
       $c++;
     }
     else {
       $a[$k] = $v;
     }
   } 
 }
 return $a;
}

DC

DeveloperChris
A: 

I don't think there is an elegant solution to this problem, you probably need to loop to the array and reorder the keys by yourself.

Alix Axel
+2  A: 

This should re-index the array while preserving string keys:

$x = array_merge($x);
Kevin
that's... that's awesome.
nickf