0

Hi all,

I have 2 multidimensional arrays which i have to print simultaneously and one of them will not have same number of elements at some times. So i check them using empty() function. As it is not possible to check every single element, i wrote a function which has to check the elements index (i pass these as arguments)and do the remaining process. But here the problem is that the function is not passing if the element is empty and this effects the other array to be printed properly.

I would be thankful if some one could tell me how to pass empty/NULL values to functions or how to make function accept the empty/NULL values to pass as arguments.

Thanking you,

Kind Regards
Aravind

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by diafol
0
<?php

function foo( $bar = null, $baz = null, $qux = null ){
     //Check if variables are not null
     //Do something with the variables
}

Usage:

<?php
foo();
foo( $bar );
foo( $bar, $baz );
foo( $bar, $baz, $qux );
foo( null, $baz );
foo( null, null, $qux );

//etc etc...
0

The call to the function looks like this:

$toCount = toCount($human["gene"]["genenames"], $mouse["gene"]["genenames"]);

and here the second array "genenames" is not created in

$mouse["gene"]["genenames"]

and the function looks like this:

function toCount($man = null, $mice = null)
		{
			if((!(empty($man))) && (!(empty($mice))))
			{
				$toCount = max(count($man), count($mice)); # take the one which has maximum entries as the counter
			}
			else if (!(empty($man)))
			{
				$toCount = count($man);
			}
			else if(!(empty($mice)))
			{
				$toCount = count($mice);
			}
			return $toCount;
		}

when i do this it shows me an error that undefined index "genenames" (as the array is not created) and this array creation depends on the user query. So what i want is if then function takes these arguments then i can check inside the function with empty() or array_exists() functions.

Thanks a lot in advance.

cheerss
Aravind

0

You can't use an array index that is not defined. Period.
You would first need to do a check of the variable to see if it exists and is set before passing it to the function.

if( empty( $mouse["gene"]["genenames"] ) ){
    $mouse["gene"]["genenames"] = array();
}

That code will check if the index is empty (http://php.net/manual/en/function.empty.php), and if it doesn't exist it just creates it as an empty array.

<?php
if( empty( $mouse["gene"]["genenames"] ) ){
	$mouse["gene"]["genenames"] = array();
}

$toCount = toCount( $human["gene"]["genenames"], $mouse["gene"]["genenames"] );
0

I tried following this thread, but got confused. Apologies if I got the wrong end of the stick:

//Initialize to prevent error on passing parameters
$man = array();
$mouse = array();
//I assume that these arrays are always created, so the above may not be needed

function toCount($man,$mouse){
  $manno = (isset($man['gene']['genenames']))? count($man['gene']['genenames']) : 0;
  $mouseno = (isset($mouse['gene']['genenames']))? count($mouse['gene']['genenames']) : 0;
  return $manno + $mouseno;
}

//dummy data - could be anything
$man['gene']['genenames'] = array('t','s');
$mouse['gene']['genenames'] = array(0,1,2,3,4,5,6,7,8);

//usage
echo toCount($man,$mouse);

@ms, you'll be able to put me straight - isset() OK in this instance?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.