PHP: reordenar matrices después de unset ()
Hay 2 funciones involucradas.
Buscar elementos de matriz para una cadena dadaelemento de matriz unset () si no se encuentra la cadena$array = array("first", "second", "third", "fourth");
foreach($array as $i=> $string) {
if(stristr($string, "e")) {
unset($array[$i]);
}
}
second
es el elemento de matriz con el carácter 'e'. Si esunset
, $array[1]
quedaría vacío:
$array[0] = "first"
$array[1] = ""
$array[2] = "third"
$array[3] = "fourth"
yo quiero$array[1]
para ser eliminado de la matriz (como enarray_shift()
), así que esothird
toma el lugar desecond
yfourth
el lugar dethird
:
$array[0] = "first"
$array[1] = "third"
$array[2] = "fourth"