1

for this array,

Array ( 
  [0] => 'HOST:' 
  [1] => 'killbill' 
  [2] =>  
  [3] =>  
  [4] =>  
  [5] =>  
  [6] =>  
  [7] => 
  [8] => 
  [9] => 
  [10] => 
  [11] => 'Loss%' 
  [12] => 
  [13] => 
  [14] => 'Snt' 
  [15] => 
  [16] => 
  [17] => 'Last' 
  [18] => 
  [19] => 
  [20] =>'id'
)

it has empty values.by using this code it gives

foreach($array as $key => $subarray) {
    $array[$key] = preg_grep('/^$/', $subarray, PREG_GREP_INVERT);
}

array ( 
  [0] => HOST: 
  [1] => killbill 
  [11] => Loss% 
  [14] => Snt 
  [17] => Last 
  [20] =>id
)

that means it removes all the spaces. but it has the original key values .(compair above one and bellow one. then can get a clear idea what i'm saying).but i want to have it like this.

array ( 
  [0] => 'HOST:' 
  [1] => 'killbill' 
  [2] => 'Loss%'
  [3] => 'Snt' 
  [4] => 'Last' 
  [5] => 'id'
)

key values as 1,2,3,4.... so how could i get that.

5
  • 4
    array_filter() without a callback will remove empty elements and retain associativity; then pass that to array_values() to reset the keys from 0 Commented Apr 2, 2013 at 11:54
  • array_filter() is a good plan, but note that you will still probably want a callback (depending on your definition of "empty") because without one it will remove any falsey value, notably it will remove '0' which you likely want to keep since it looks like you are working with ping results. Commented Apr 2, 2013 at 11:56
  • this is a question that i have to faced from @Felix Kling 's answer. answered Apr 21 '11 at 22:38- [link]stackoverflow.com/questions/5750407/… Commented Apr 2, 2013 at 11:59
  • @Thusitha Sumanadasa my answer will help you. Commented Apr 2, 2013 at 12:01
  • thanks oliver A .for editing this nicely Commented Apr 2, 2013 at 12:23

5 Answers 5

6

simply use this instead of Foreach

array_values(array_filter($array));

that will remove the space and reorder your array.

look: http://codepad.org/howl3Opj

Sign up to request clarification or add additional context in comments.

1 Comment

anytime... :D, Happy coding !
1

Just use array_filter().

$array = array_filter($array);

That will remove all the empty values -- ie blank, null, false, zero.

If you only want to remove values that are empty strings and keep other empty values (eg zero), you can specify the second parameter for array_filter(), which allows you to define a function to specify which elements should be filtered.

$array = array_filter($array, function($val) {return $val!=='';});

Hope that helps.

3 Comments

@Spudley that part of code is not working $array = array_filter($array, function($val) {return $val!=='';}); , giving error Parse error: syntax error, unexpected T_FUNCTION
@vikastyagi - what version of PHP are you using? This code requires PHP v5.3 or higher. The error you've given looks like you're trying to run it in PHP 5.2.
@Spudley you are right, code is working on php version 5.3 thanks
0

try this function it will help you to sort out the issue

$arr = array_map('array_values', $arr);

Comments

0

Use array_diff function

<?php

   $array_space = array(0,3,4,45,12,"",54,23);

   $remove = array("");

   print_r(array_diff($array_space,$remove));

?>

See Output here

Comments

0

at start you need to take the array i gave him a name $arrWords

$arrWords = array_filter($arrWords, 'trim');

after i clean all the empty spaces i will make new array keys

$arrWords = array_values($arrWords);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.