/ Published in: PHP
                    
                                        
A utility function to preserve an array in a session. Adding the newest element to the beginning of the array and popping out the last one. Useful for displaying for instance; your last searches option in your website.
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
// FUNCTION : array_stack_session
// RETURNS : the manipulated session array
// USAGE:
<?php
$_SESSION['last_searches'] = array_stack_session("newItem", $_SESSION['last_searches']);
?>
function array_stack_session( $element, $array_session, $limit = 5 ) {
if ( !(is_array($array_session)) ) {
$array_session = array();
}
array_unshift($array_session, $element);
if ( sizeof($array_session)>$limit ) {
array_pop($array_session);
}
return $array_session;
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                