a function to remove a value from an array ?
Moderator: General Moderators
-
VisionsOfCody
- Forum Commoner
- Posts: 30
- Joined: Sat Mar 01, 2003 1:19 pm
- Location: France
a function to remove a value from an array ?
Hi
i've had a look at the list of functions starting with "array_" in the manual and didn't find a function that would enable me to completely remove a value from an array (using it's position) and then move up the following values to 'fill the gap', like this :
so my array is, for example,
$list=array("apple", "banana", "grape", "orange");
i'd like to be able to remove the value $list[1] and move up the next value "grape" to take its place
this must be possible - does anyone know the function to do it?
thanks
i've had a look at the list of functions starting with "array_" in the manual and didn't find a function that would enable me to completely remove a value from an array (using it's position) and then move up the following values to 'fill the gap', like this :
so my array is, for example,
$list=array("apple", "banana", "grape", "orange");
i'd like to be able to remove the value $list[1] and move up the next value "grape" to take its place
this must be possible - does anyone know the function to do it?
thanks
i don't know of a function, but it's not hard to write one....
let's seee... starteing at 12:34 pm my time
let's seee... starteing at 12:34 pm my time
Code: Select all
function remove_array_element($array, $position){
$array2=array();
for($i=0;$i<$position;$i++){
$array2[]=$array[$i];
}
for($i=($position+1);$i<(count($array)+1);$i++){
$array2[]=$array[$i];
}
return $array2;
}-
VisionsOfCody
- Forum Commoner
- Posts: 30
- Joined: Sat Mar 01, 2003 1:19 pm
- Location: France
lordy!
that was quick!
i was thinking i'd have to do something like that but it seemed a very daunting task - thank you
i was thinking i'd have to do something like that but it seemed a very daunting task - thank you
Cant test as I'm at work, but if this works, it would be easier...
Code: Select all
// destroys a single element
unset ($list[1]);-
VisionsOfCody
- Forum Commoner
- Posts: 30
- Joined: Sat Mar 01, 2003 1:19 pm
- Location: France
ahaaaaa !!!
i'll give that a try - if it works then the world will be a brighter place
thanks
thanks
-
VisionsOfCody
- Forum Commoner
- Posts: 30
- Joined: Sat Mar 01, 2003 1:19 pm
- Location: France
true. unset is only meant to remove the value somehting is set at, since it can be used on a string or int, it would not make sens that it would bump things up.
i figured 2 for loops would be easier to follow than
i figured 2 for loops would be easier to follow than
Code: Select all
for($i=$place;$i<count($array);$i++){
$array[$i]=$array[($i+1)];
}
unset($array[count($array)]);
Last edited by m3rajk on Mon Sep 01, 2003 8:45 pm, edited 1 time in total.
How about a function which does just one thing ie resets keys in a numerical array?
It's better from a design point of view to give functions a single task. The advantage here is that now we have a little utility function which might have other uses (change an associative array to numerical for example). The $i arg also adds some flexibility so you can specify any integer for the first key.
Note I didn't run this through the parser.
My aim here was to think a little outside the original problem and adapt the solution so that it can be applied to other similar problems without having to create more code. We end up with a utility fn possibly with other uses and a fn which will unset not just one but multiple elements. This particular example is kind of small beer really but it's a good principle to follow.
It's better from a design point of view to give functions a single task. The advantage here is that now we have a little utility function which might have other uses (change an associative array to numerical for example). The $i arg also adds some flexibility so you can specify any integer for the first key.
Code: Select all
<?php
// reset array keys in sequence, $i is the first key
function resetNumericalKeys(&$array, $i)
{
foreach($array as $value)
{
$array[$i] = $array[$value];
$i++;
}
}
// note passing by ref so, in use:
unset($array[$key]);
resetNumericalKeys($array, $i);
array_pop($array);
// We could stop right there but let's extend that further, to create a wrapper function which will delete any number of elements from a numerical array & reset the keys in sequence.
// "$keys" arg is an array of the keys to be deleted, $i the start key of the new array, as above
function unsetValuesResetKeys(&$array, $keys, $i)
{
foreach($keys as $value)
{
unset($array[$value]);
resetNumericalKeys($array, $i);
array_pop($array);
}
}
// this may be slightly inefficient in the sense that ideally we'd have a setup where we reset array keys just once rather than once per loop. In practice it won't really matter.
?>My aim here was to think a little outside the original problem and adapt the solution so that it can be applied to other similar problems without having to create more code. We end up with a utility fn possibly with other uses and a fn which will unset not just one but multiple elements. This particular example is kind of small beer really but it's a good principle to follow.
Last edited by McGruff on Wed Aug 10, 2005 8:09 pm, edited 1 time in total.
There are some built-ins that can restore the continuity of numerical indices, e.g. and hole or not foreach will work
Code: Select all
<?php
$arr = array(1,1,2,2);
unset($arr[1]);
$arr = array_values($arr);
print_r($arr);
?>Code: Select all
<?php
$arr = array(1,1,2,2);
unset($arr[1]);
foreach($arr as $val)
echo $val, "\n";
?>-
VisionsOfCody
- Forum Commoner
- Posts: 30
- Joined: Sat Mar 01, 2003 1:19 pm
- Location: France
I had to do the same thing recently. This is how I solved the problem.
Code: Select all
unset($array[$offset]);
array_splice($array, 0);
// Now there is no more hole!