Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

php - 删除数组php中的空格(remove spaces in a array php)

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.

(但它具有原始键值。(将其替换为大于1的值,然后按下面的波纹即可。然后我可以清楚地知道我在说什么)。但是我想要这样。)

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 ....所以我怎么能得到它。)

  ask by Thusitha Sumanadasa translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(外观: http//codepad.org/howl3Opj)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...