• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • 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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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