Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Remove elements with specific key from multidimensional array?

    Hi,

    If I have a multidimensional array e.g.


    PHP Code:
    $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

  • #2
    New Coder
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post
    PHP Code:
    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..

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    If you want to remove the whole of the fruit array including the number use this
    PHP Code:
    $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($mdatrue).'</pre>'
    That code will just leave the third array with the VEG
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    thanks both - I would never have figured that out lol!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •