Forum Moderators: coopster

Message Too Old, No Replies

delete values from a multi array

         

SteveD

11:56 am on Mar 22, 2005 (gmt 0)

10+ Year Member



Hi,
I am having a (small) problem with deleting values from a multi dimensional array, I am hoping that it is a silly syntax prob
I have two arrays :
the first is called $delete and is created from user input

Array ( [Formula 2 Flake] => Array ( [0] => 34g ) [Formula 2 Medium Pellet] => Array ( [0] => 400g ) )

second array is called $list and come from db extraction

Array ( [Formula 2 Flake] => Array ( [0] => 34g [1] => 70g ) [Formula 2 Medium Pellet] => Array ( [0] => 100g [1] => 200g [2] => 400g ) )

I want to delete the values 34g and 400g from $list

foreach($list as $k => $v)
{
foreach($v as $v1 => $v2)
{
if(in_array(array($v2),$delete))
{
echo"$v2 is in delete<br />";
unset($list[$v2]);
}
}
}

The problem here ( I think) is that unset is addressing a flat array, after many hours searching ( php.net etc) I cannot find, or work out what the proper syntax should be.

Any help greatly appreciated

Many Thanks
SteveD

ironik

1:08 am on Mar 23, 2005 (gmt 0)

10+ Year Member



You should probably use foreach on the delete array:


foreach ($delete as $item=>$weight)
{
if (isset($list[$item][$weight]))
{
unset($list[$item][$weight]);
}
}

If it finds the same value in $list as in $delete then it removes the item from $list.

Hope that works, I'm tired!

SteveD

10:42 am on Mar 23, 2005 (gmt 0)

10+ Year Member



Hi,
Thanks for your response, unfortunately it doesn't work,
firts the code needed an extra foreach in order to identify the correct weight and product name, that now works fine, far more acurately than my original, so thank you for that, however the unset is still not working, I have tried different variations of the unset and array_splice, but to no avail, I shall keep trying, if you have any other ideas would love to hear them.
this is the final code(with the unset not working);

foreach($delete as $name =>$a)
{
foreach($a as $k => $weight)
{
if(isset($list,$weight))
{
echo "itme = $name :: weight = $weight<br />";
unset($list[$name][$weight]);
}
}
}

Cheers

Steve

mincklerstraat

2:07 pm on Mar 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you want to know whether a certain value is in your array (isset() will only work for keys, in_array() won't tell you which key), you need to use
array_search()
, which will give you the key, which you can then delete by using unset($array[$key]), or if you're not in the global namespace, unset($GLOBALS[$array][$key]).

SteveD

3:25 pm on Mar 23, 2005 (gmt 0)

10+ Year Member



Hi,
Yes I agree, I had looked at array_search, but like the previous code I was having difficulty accessing the correct key of a multi-dimensional array, thanks to your prompt I had another look and lo and behold, I have a result, final code is :
foreach($delete as $name =>$a)
{
foreach($a as $k => $weight)
{
if(isset($list,$weight))
{
$pos = array_search($weight,$list[$name]);
unset($list[$name][$pos]);
}
}
}

Thank you both for your input

Cheers

Steve

ironik

9:07 pm on Mar 23, 2005 (gmt 0)

10+ Year Member



D'oh! ... did I just give an example using the arrays item as a key?! There's a moral there.. don't write code when your tired! Looking back it looks completely non-sensical... sorry SteveD!