删除数组php中的空格

时间:2021-06-01 21:41:23

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.

关键值为1,2,3,4 ....所以我怎么能得到它。

4 个解决方案

#1


3  

simply use this instead of Foreach

只需使用此而不是Foreach

array_values(array_filter($array));

that will remove the space and reorder your array.

这将删除空间并重新排序您的阵列。

look: http://codepad.org/howl3Opj

#2


1  

Just use array_filter().

只需使用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_filter()指定第二个参数,该参数允许您定义一个函数以指定应过滤哪些元素。

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

Hope that helps.

希望有所帮助。

#3


0  

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

尝试此功能,它将帮助您解决问题

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

#4


0  

Use array_diff function

使用array_diff函数

<?php

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

   $remove = array("");

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

?>

See Output here

请参见此处的输出

#1


3  

simply use this instead of Foreach

只需使用此而不是Foreach

array_values(array_filter($array));

that will remove the space and reorder your array.

这将删除空间并重新排序您的阵列。

look: http://codepad.org/howl3Opj

#2


1  

Just use array_filter().

只需使用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_filter()指定第二个参数,该参数允许您定义一个函数以指定应过滤哪些元素。

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

Hope that helps.

希望有所帮助。

#3


0  

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

尝试此功能,它将帮助您解决问题

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

#4


0  

Use array_diff function

使用array_diff函数

<?php

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

   $remove = array("");

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

?>

See Output here

请参见此处的输出