1

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] =>
) 
6
  • tried but keys are going different. Array ( [0] => [1] => [2] => [3] => PHM [4] => PHN [5] => N [6] => M9 ) Commented Jun 16, 2016 at 12:34
  • How have you used the function? Commented Jun 16, 2016 at 12:35
  • array_slice($table_list, $filter); Commented Jun 16, 2016 at 12:36
  • Well just do: print_r(array_slice($table_list, $filter)); Commented Jun 16, 2016 at 12:38
  • 1
    Your array starts with 1 and not 0, so you have to set the argument to true to keep the index, so: print_r(array_slice($table_list, $filter, NULL, TRUE)); Commented Jun 16, 2016 at 12:45

2 Answers 2

7

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));
Sign up to request clarification or add additional context in comments.

Comments

2
$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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.