0
<?php
    session_start();    
    $_SESSION['del']=array("a1","a2","a3","a4","a5");
    unset($_SESSION['del'][0]);
    echo implode(" ",$_SESSION['del'])    
?>

How do I remove each array element one by one every time I refresh the page?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • 1
    This code won't work as you expect because you're reinitialising the array before you unset the first element. Every load will show the same result. Reorganise the code to fix this and try again. –  Sep 12 '13 at 01:48
  • sorry, i'm new to php , also new to here too. i don't know how to fix my code. that is why i'm trying to get some helps from here. THanks for your answer and time. – facebook man Sep 12 '13 at 01:51

3 Answers3

1

here is the code

<?php
session_start();    
if(isset($_SESSION['del'])) {  // to make sure array is not set again as in question
    unset($_SESSION['del'][0]); // remove the first element
    $_SESSION['del'] = array_values($_SESSION['del']); // to shift rest of the elements one location left for making indexes starts from 0
} else { // set session array only once
    $_SESSION['del']=array("a1","a2","a3","a4","a5");
}
echo implode(" ",$_SESSION['del']); // print results    
?>
dkkumargoyal
  • 556
  • 3
  • 10
1
if(isset($_SESSION['del']))
{
 if(is_array($_SESSION['del']))
 {
  array_shift($_SESSION['del']);
 }
}

Code Explanation

if the session del is currently set.

and that the session['del'] is an array

remove the first value of the array del.

Nicolas Racine
  • 1,031
  • 3
  • 13
  • 35
0

I am not sure if it works with $_SESSION, but if it is an array then array_shift() would be what you are looking for:

array_shift($_SESSION['del']);
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52