HI I got so many empty element in array( contain only whitespace). anyone know how to remove it?
$my[0] =
$my[1] = 5.21
$my[2] = 425
$my[3] =
$my[4] =
$my[5] = 75

I want to remove 0,3,4,5. I did try several way but really get stuck. Anyone help me please.

    Hi Dollar,

    This is my first time replying so be gentle, try using the unset function like this

    $my = array();
    $my[0] = null; 
    $my[1] = 5.21; 
    $my[2] = 425; 
    $my[3] = null; 
    $my[4] = null; 
    $my[5] = 75; 
    foreach ($my as $key => $value) {
      if (is_null($value)) {
        unset($my[$key]);
      }
    }
    foreach ($my as $key => $value) {
      echo   "  $key : $value <br>";
    }
    //....out put.....
    1 : 5.21 
    2 : 425 
    5 : 75 
    
    //but if you want to re-order the array lets try this, only draw back
    //is we are creating a new array
    $newMy = array();
    $i = 0;
    foreach ($my as $key => $value) {
      if (!is_null($value)) {
    	$newMy[$i] = $value;
    	$i++;
      }
    }
    foreach ($newMy as $key => $value) {
      echo   "  $key : $value <br>";
    }
    //....out put....
    0 : 5.21 
    1 : 425 
    2 : 75 
    
    

    I really hope this helps you a little, I actually would like to know how we keep the same array if its possible?

    BBK
    🙂

      foreach($array as $key => $value) {
        if($value == "") {
          unset($array[$key]);
        }
      }
      $new_array = array_values($array);
      

      and voila, $new_array should only contain entries that are not empty & the indexes of the array are reordered so that they are not missing indexes!

        Thank you so much, you guys. It works very well. thank you..Dollar

          Hi dollar,

          just be very careful with "" and null as they are two different things, eg look at this array below

          $my = array();
          $my[0] = null; 
          $my[1] = 5.21; 
          $my[2] = 425; 
          $my[3] = null; 
          $my[4] = ""; 
          $my[5] = 75; 
          
          //note element [4] is empty not a null value
          
          $newMy = array();
          $i = 0;
          foreach ($my as $key => $value) {
            if (!is_null($value)) {
          	$newMy[$i] = $value;
          	$i++;
            }
          }
          foreach ($newMy as $key => $value) {
            echo   "  $key : $value <br>";
          }
          //....out put ....
          0 : 5.21 
          1 : 425 
          2 : 
          3 : 75 
          
          /*
          so we still have a empty element at [2]
          but if we change the line below
          if (!is_null($value)) {
          with this one 
          if ((!is_null($value)) && ($value !== "")) {
          our out put is this
          0 : 5.21 
          1 : 425 
          2 : 75 
          
          */
          
          

          whats important here is that null and "" are treated as two different values, not sure if you knew this.

          Have fun

          BBK
          🙂

            4 years later

            [MOD]Before anyone else replies, please note that this is an over 4-year-old thread. -- NogDog[/MOD]

            If you also want to delete values which are not empty but equal to "" then just use this foreach:

            	foreach ($myarr as $key => $value) { 
            	  if (is_null($value) || $value=="") { 
            		unset($myarr[$key]); 
            	  } 
            	} 
            

            By adding or $value=="" to the if function he will also remove the empty strings 😉.

              14 days later
              6 months later

              [MOD]Before replying to this thread, note the NogDog's edit made about half a year ago. -- Weedpacket[/MOD]

              Here remove the empty values and arraged the array in correct order

              $ii=0;
              foreach($list as $key_null=>$val_null){
              if($val_null){
              $list1[$ii]=$val_null;$ii++;
              }
              }

              by saami

              [MOD]Besides, array_values(array_filter($list)).[/MOD]

                15 days later

                Hey gang,

                Could I get someone to explain what this line is doing. I'm learning php fast, but still have a ways to go! 😉

                " $key => $value "

                Here's the context (from this tread, c. 2003):

                foreach ($thisArray as $key => $value) {
                if (is_null($value) || $value == "") {
                unset ($thisArray[$key]);
                }
                }

                The part I'm having trouble understanding is the " => " I don't see it listed as an operator in the php list on devguru.com (my hero, btw). Is this just a way of drilling-down into an associative array?

                Also, why is it necessary to act on "$thisArray" as "$key"? Why not just look directly at $thisArray?

                Thanks in advance for any help!

                Newbie 😉

                  Shane10101 wrote:

                  Is this just a way of drilling-down into an associative array?

                  Yes. $key isn't the same thing as $thisArray. It's one key from the array. The page you want to look at is the one in the PHP manual on [man]foreach[/man].

                    🙂 I'm still stuck on the "=>" part, but that helps.

                      I'm still stuck on the "=>" part

                      It is just array syntax to denote key => value. As such, you see it used for foreach loops, and when initialising an associative array:

                      $array = array('key1' => 'value1', 'key2' => 'value2');
                        7 days later
                        2 years later

                        We can use this function to remove blank values from an array.

                        public function __removeBlankValues($array_val=array()){
                        $returnArray = array();
                        foreach($array_val as $key => $val){
                        if(trim($val) != ''){
                        $returnArray[$key] = $val;
                        }
                        }
                        return $returnArray;
                        }

                        For More please visit on:

                        http://www.jvdinfoways.com

                          13 days later
                          Dollar;10386574 wrote:

                          HI I got so many empty element in array( contain only whitespace). anyone know how to remove it?
                          $my[0] =
                          $my[1] = 5.21
                          $my[2] = 425
                          $my[3] =
                          $my[4] =
                          $my[5] = 75

                          I want to remove 0,3,4,5. I did try several way but really get stuck. Anyone help me please.

                          i'm guessing you want to remove 0, 3, 4 and leave the 5...

                          can be done in 1 line:

                          array_flip(array_flip($my);

                            $array = array_values($array);

                              dagon;10963300 wrote:

                              $array = array_values($array);

                              <?php
                              $array = array(null,'1','2',null,'3',null);
                              $array = array_values($array);
                              print_r($array);
                              ?>

                              result:
                              Array
                              (
                              [0] =>
                              [1] => 1
                              [2] => 2
                              [3] =>
                              [4] => 3
                              [5] =>
                              )

                                sorry, bad memory, its array_filter()

                                <?php
                                $array = array(null,'1','2','','3',null);
                                 $array=array_filter($array);
                                print_r($array);
                                
                                ?>
                                

                                Array
                                (
                                [1] => 1
                                [2] => 2
                                [4] => 3
                                )

                                  a month later
                                  dopp;10386720 wrote:
                                  foreach($array as $key => $value) {
                                    if($value == "") {
                                      unset($array[$key]);
                                    }
                                  }
                                  $new_array = array_values($array);

                                  This could not be more beautiful 🙂

                                  foreach ($my as $k => $v){
                                  if ($my[$k]==""){unset($my[$k]);}}
                                  $my=array_values($my);

                                    TryPHP: it looks ugly when it is all bunched up like that.

                                    Anyway, this thread appears to have an unnecessary amount of resurrection. If you are reading this and have anything else to say about removing an "empty" element from an array, feel free to create a new thread with your own requirements.

                                    thread closed

                                      Write a Reply...