...

View Full Version : Remove elements with specific key from multidimensional array?



htcilt
01-25-2010, 10:40 AM
Hi,

If I have a multidimensional array e.g.



$mda = array(
array(0,"FRUIT","BAN","Banana"),
array(1,"FRUIT","APP","Apple"),
array(2,"VEG","POT","Potato")
);

How would I remove every type of fruit?
If sure its simple but I just can't figure it out :(

jorgenfi
01-25-2010, 12:27 PM
for ($i = 0, $i<length($mda),$i++) {
for ($j = 1, $j<length($mda[$i]),$j++) {
unset $mda[$i][$j];
}
}


maybe this works, haven't testet it..

JAY6390
01-25-2010, 01:24 PM
If you want to remove the whole of the fruit array including the number use this

$mda = array(
array(0,"FRUIT","BAN","Banana"),
array(1,"FRUIT","APP","Apple"),
array(2,"VEG","POT","Potato")
);

foreach($mda as $k=>$v) {
if($v[1] == 'FRUIT') {
unset($mda[$k]);
}
}
echo '<pre>'.print_r($mda, true).'</pre>';That code will just leave the third array with the VEG

htcilt
01-25-2010, 03:01 PM
thanks both - I would never have figured that out lol!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum