PHP:重新排序后未设置阵列() [英] PHP: Reorder arrays after unset()

查看:153
本文介绍了PHP:重新排序后未设置阵列()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有涉及2个功能。


  1. 对于一个给定的字符串数组搜索项

  2. 如果字符串没有找到未设置()数组项


    $阵列=阵列(第一,第二,第三,第四);

 的foreach($数组作为I $ => $字符串){
 如果(stristr($字符串,E)){
    未设置($数组[$ i]);
 }}

第二与字符E的数组项。如果未设置它,$阵列[1]将空

  $数组[0] =第一
$阵列[1] =
$阵列[2] =第三
$阵列[3] =第四

我想$阵列[1]从数组中删除(像array_shift()),从而使第三发生的地方第二和第四的地方第三

  $数组[0] =第一
$阵列[1] =第三
$阵列[2] =第四

感谢


解决方案

  $阵列= array_values​​($数组);

There are 2 functions involved.

  1. Search array items for a given string
  2. unset() array item if string not found

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

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

}

"second" is the array item with the character 'e'. If its unset, $array[1] would be left empty

$array[0] = "first"  
$array[1] = ""  
$array[2] = "third"  
$array[3] = "fourth"

I want $array[1] to be removed from the array (like in array_shift() ), so that "third" takes the place of "second" and "fourth" the place of "third"

$array[0] = "first"    
$array[1] = "third"  
$array[2] = "fourth"

thanks

解决方案

$array = array_values($array);

这篇关于PHP:重新排序后未设置阵列()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆