shadowline
Accessing Arrays << Using Arrays >> Sorting Arrays
This is a function that I find very useful while working with arrays, especially for filtering certain items.

Usage: in_array(search word, array)

This is useful because when you want to find a value easily, you can use this. Also, it is useful for maybe blocking certain things, check this example:

<?php
$fruit = array("apple","orange","strawberry");
if(in_array("apple", $fruit))
{
echo "Apple is in the array";
}
else
{
echo "Apple is not in the array";
}
?>

This would return "Apple is in the array"

Another use for this would be blocking things, such as IP addresses from accessing your site, here is an example:

<?php
$blocked = array("192.168.1.11", "192.168.1.12", "192.168.1.13");
$ip = $REMOTE_ADDR;
if(in_array($ip, $blocked))
{
echo "You have been banned from this site";
}
else
{
//normal page code here
}
?>

If the person has the IP address of any of the three in the array, they will receive the banned message, otherwise, they will see the normal content that everyone else will see.

Tutorial contributed by : Jeremy Tymes
Accessing Arrays << Using Arrays >> Sorting Arrays
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow