Function : Check if array index is set and whether it has a value.
Array Index Set
This function lets you determine whether an array index is set and whether it has a value. It takes array item, array and default value as parameters. If the element is empty it returns FALSE or whatever you specify as the default value.
<?php
/**
* Array Index Set - Lets you determine whether an array index is set and whether it has a value.
*
* If the element is empty it returns FALSE (or whatever you specify as the default value.)
* @param string - $default
* @param array - $array
* @param mixed - $item
* @return mixed depends on what the array contains
*/
if ( ! function_exists('element'))
{
function element($item, $array, $default = FALSE)
{
if ( ! isset($array[$item]) OR $array[$item] == "")
{
return $default;
}
return $array[$item];
}
}
?>
Usage :
<?php
$array = array("a"=>"apple", "b"=>"bat","c"=>"cat");
$x = element("a", $array); //returns apple
$x = element("x",$array); //returns false
$x = element("b",$array,"ball"); //returns ball
$x = element("n",$array,"Not Found"); //returns Not Found
?>












[...] This post was mentioned on Twitter by Abhishek Sanghvi. Abhishek Sanghvi said: Function : Check if array index is set and whether it has a value. – http://tinyurl.com/2buh9z8 [...]