Return to Snippet

Revision: 23107
at January 30, 2010 01:44 by Heroftime


Initial Code
// 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;

}

Initial URL

                                

Initial Description
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.

Initial Title
A utility function to preserve an array in a session.

Initial Tags
mysql, php, array

Initial Language
PHP