PHP: Reorder arrays after unset()

Zeb*_*bra 37 php arrays

There are 2 functions involved.

  1. 搜索给定字符串的数组项
  2. 如果找不到字符串,则为unset()数组项

$array = array("first", "second", "third", "fourth");

foreach($array as $i=> $string) {  
 if(stristr($string, "e")) {  
    unset($array[$i]);
 }   

}
Run Code Online (Sandbox Code Playgroud)

second是具有字符"e"的数组项.如果它unset,$array[1]将留空:

$array[0] = "first"  
$array[1] = ""  
$array[2] = "third"  
$array[3] = "fourth"
Run Code Online (Sandbox Code Playgroud)

我想$array[1]从阵列(如在除去array_shift()),从而使third需要的地方secondfourth的地方third:

$array[0] = "first"    
$array[1] = "third"  
$array[2] = "fourth"
Run Code Online (Sandbox Code Playgroud)

Mat*_*hew 91

$array = array_values($array);
Run Code Online (Sandbox Code Playgroud)