Php数组反向删除

Php数组反向删除

问题描述:

I need to delete the values inside the array that are before the given variable and store in the same key, thanks in advance.

$filter = 8;

Array
(
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => 
    [8] => 
    [9] => 
    [10] => 
    [11] => 
    [13] => PHM
    [14] => PHN
    [15] => N
    [17] => M9
    [18] =>

)

My output should be,

Array(
    [8] => 
    [9] => 
    [10] => 
    [11] => 
    [13] => PHM
    [14] => PHN
    [15] => N
    [17] => M9
    [18] =>
) 

我需要删除数组中给定变量之前的值并存储在同一个键中,谢谢 提前。 p>

  $ filter = 8; 
 
Array 
(
 [1] => 
 [2] => 
 [3] =  > 
 [4] => 
 [5] => 
 [6] => 
 [7] => 
 [8] => 
 [9] =>  
 [10] => 
 [11] => 
 [13] => PHM 
 [14] => PHN 
 [15] => N 
 [17] =>  ; M9 
 [18] => 
 
)
  code>  pre> 
 
 

我的输出应该是, p>

 数组(
 [8] => 
 [9] => 
 [10] => 
 [11] => 
 [13] => PHM 
 [14] =  > PHN 
 [15] => N 
 [17] => M9 
 [18] => 
)
  code>  pre> 
  div>

You can use array_slice() to do this. Now since you don't have a 0-based enumerated array, you have to make sure to set the preserve_keys parameter to true:

print_r(array_slice($table_list, $filter, NULL, TRUE));

$arr = array("","","","","","","","","","","","","","PHM","PHN","N","","M9","");
$filter = 8;
For($i=0;$i<$filter;$i++){
    If(isset($arr[$i])) unset($arr[$i]);
}

Var_dump($arr);

I created an array with all indexes, for this example, but it will work even if 0 and 12 is missing because of isset().

https://3v4l.org/WKMGO

Edited to keep key 8.