0

I have a session variable like this:

$list = Session::get('list_id')

the $list variable value has a value of Array ( [0] => A, [1] => B,[2] => C, [4] => D)

when I am using unset($list [0]);

I am getting a value of Array ([1] => B,[2] => C, [4] => D)

The problem that I am getting is all of my parameters has a parameters of $list[0] I need the index 0 always, so that value that I am looking for after I remove the index 0 is Array ([0] => B,[1] => C, [2] => D)

so that I can store it again on my session.

is there a way to do this?

2 Answers2

0

Use array_values function if you are sure your array has only numerical index keys.

See this link for more details; https://www.php.net/manual/en/function.array-values.php

cednore
  • 874
  • 4
  • 20
  • it has already been answered many times. Please flag it as a duplicate. https://stackoverflow.com/questions/11224821/how-to-reindex-an-array – Deepesh Thapa Oct 14 '20 at 08:13
0

Do not use unset. array_shift — Shift an element off the beginning of array

$removedElement = array_shift($yourArray);

array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down.

Sher Singh
  • 279
  • 3
  • 13