• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

Make a function handle arrays

I want the following function to be able to accept arrays, or a single value.

I am implementing this into an existing enterprise application, so it needs to be bulletproof, if you know what I mean.

I would ideally like it to handle:

a. a single value
b. an array
c. null ( do nothing )

Thanks!
// unset a value
function ss_clear_info($key)
{
	unset($_SESSION[$key]);
}

Open in new window

0
edvinson
Asked:
edvinson
  • 3
  • 3
1 Solution
 
artmsCommented:
How it supposed to handle array? With each array element it should unset that value from $_SESSION global array?
0
 
edvinsonAuthor Commented:
I would like the function to be able handle both of these scenerios ( for backward compatibility ):

ss_clear_info($aSingleKey)

ss_clear_info($one, $two, $third, $etc, $etc)

and finally

ss_clear_info($myArrayOfKeysToClear)

0
 
edvinsonAuthor Commented:
I think I came up with my own solution...
function ss_clear() {
 
	$numargs = func_num_args();
	
	if($numargs>0) {
 
		$arg_list = func_get_args();
 
		for ($i = 0; $i < $numargs; $i++) {
       		if(gettype($arg_list[$i] == 'array')){
				foreach ($arr as $key => $value) {
    				unset($_SESSION[$key]);
				}
		    } else {
				unset($_SESSION[$key]);
			}
   		}
		
		return true;
		
	} else {
		
		return false;
		
	}
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
artmsCommented:
To bullet proof solution you should also check if array in array is not passed or this type can be simply ignored...
0
 
edvinsonAuthor Commented:
Ok, cool. since i have to give the points to someone, could you show me , so i can award you the points?
0
 
artmsCommented:
You can close the ticket by not giving anyone points (well nobody helped, you helped yourself :) ). Check options or ask moderators for help how to close it.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now