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
?>
Related posts:
- PHP : Array Functions Tutorial – Part 3
- PHP : Array Functions Tutorial – Part 2
- PHP : Array Functions Tutorial – Part 1
- Function : Converts a MySQL Timestamp to Unix
- Function : Converts GMT time to a localized value
- Function : Converts a local Unix timestamp to GMT
- Function : Get number of days in month
- Function: Get Random Element from Array
- What is an Array? Functions of Array- Array Tutorials
- How to Get the Thumbnail of the First Image from the Post and Display it in WordPress
Related Posts
1 Comment + Add Comment
Leave a comment
Recommended
Related Posts
- PHP : Array Functions Tutorial – Part 3
- PHP : Array Functions Tutorial – Part 2
- PHP : Array Functions Tutorial – Part 1
- Function : Converts a MySQL Timestamp to Unix
- Function : Converts GMT time to a localized value
- Function : Converts a local Unix timestamp to GMT
- Function : Get number of days in month
- Function: Get Random Element from Array
- What is an Array? Functions of Array- Array Tutorials
- How to Get the Thumbnail of the First Image from the Post and Display it in WordPress

An article by





[...] 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 [...]