Jump to content

Remove an element from a multi-dimensional array


BerbBrown

Recommended Posts

Hi All

 

I have the following multi-dimensional array and I would like to remove all the 'VOID' elements.

 

I would like to maintain the array indexes.  If I had to I would like to know how I can reset the indexes.

 

Thank you very much in advance.

 

BB

 

 

 

Array
(
[0] => Array

    (
         [0] => VOID
         [1] => VOID
         [2] => VOID
         [3] => VOID
         [4] => VOID
         [5] => 16
         [6] => VOID
         [7] => 11
   
)

[1] => Array
    (
         [0] => 21
         [1] => 2
         [2] => 2
         [3] => 1
         [4] => 33
         [5] => 27
         [6] => 32
         [7] => VOID
   
)

[2] => Array
    (
         [0] => VOID
         [1] => VOID
         [2] => VOID
         [3] => VOID
         [4] => VOID
         [5] => VOID
         [6] => VOID
         [7] => VOID
    )

[3] => Array
    (
         [0] => VOID
         [1] => VOID
         [2] => VOID
         [3] => VOID
         [4] => 131
         [5] => 117
         [6] => 183
         [7] => VOID
     )

[4] => Array
     (
         [0] => VOID
         [1] => VOID
         [2] => VOID
         [3] => VOID
         [4] => VOID
         [5] => 41
         [6] => 1
         [7] => VOID
      )

)

 

Link to comment
Share on other sites

if the values are string void

<?php
     foreach($yourArray as $k0=>$innerArray){
             foreach($innerArray as $k1=>$value){
                   if(strtolower($value) === 'void') unset($yourArray[$k0][$k1]);
             } 
             # will reindex array:
             $yourArray[$k0] = array_values($yourArray[$k0]);
     }

if the values are actually null

<?php
     foreach($yourArray as $k0=>$innerArray){
             foreach($innerArray as $k1=>$value){
                   if($value == null) unset($yourArray[$k0][$k1]);
             } 
             # will reindex array:
             $yourArray[$k0] = array_values($yourArray[$k0]);
     }

You could also look into the array_filter() function  http://us2.php.net/array_filter

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.