0

I have a few session arrays which I happen to be removing specific indexes from. For example, I have a session named $_SESSION['products'], this session has these elements: $_SESSION['products'][0], $_SESSION['products'][1], and $_SESSION['products'][2].

I am trying to remove any one of those variables, the problem is when you remove the second variable, you mess up the array so that it cannot be displayed in a for loop. Is there a way to rearrange the following: $_SESSION['products'][0] and $_SESSION['products'][2] to $_SESSION['products'][0] and $_SESSION['products'][1] with a PHP built-in function? If not, is it even possible?

4

1 回答 1

1

array_values 您可以通过以下方式实现此目的:

unset($_SESSION['products'][2]); // assuming the product key 
                                 // exist in product array sess


$_SESSION['products'] = array_values($_SESSION['products']);

手动的

array_values() returns all the values from the array and **indexes the array numerically.**

于 2013-10-28T18:07:58.510 回答