![]() |
|
|||||||
| Register | FAQ | Members List | Downloads | Calendar | Today's Posts | Search | Webmaster Resources | Webmaster Blogs |
![]() UK Web Hosting |
![]() Website Hosting |
![]() UK One Way |
|
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
||||
|
If want to search an array to see if it currently holds a particular value or not, then you have a couple options which you can use :
1) in_array function in_array function takes two required arguments, first is the value which is being searched and second is the array which is to be searched for the presence of that particular value. in_array function will return true if it finds the value in the array otherwise it returns false. If the value being searched is a string then comparison will be case-sensitive. Also, note that in_array uses == by default for comparison, so it will match 0 to "invalidnumberstring" for example. If you want in_array to use === for comparison, you can indicate this to in_array by passing a boolean value true as third argument. Here's an example : Code:
<?php
$array1 = array(1, 2, 3, 4, 5, 6, 'joe');
if(in_array(1, $array1))
{
echo '1 is present in the array<br />';
}
if(in_array(0, $array1))
{
echo '0 is present in the array (when not passing third argument)<br />';
}
if(in_array(0, $array1, true))
{
echo '0 is present in the array (when passing third argument)<br />';
}
else
{
echo '0 is not present in the array (when passing third argument)<br />';
}
if(in_array('JOE', $array1))
{
echo 'JOE is present in the array<br />';
}
else
{
echo 'JOE is not present in the array<br />';
}
?>
Quote:
|
|
||||
|
2) array_search function
array_search function is similar to in_array function takes same argument and behaves exactly like in_array function except for the value it returns. Unlike in_array function, array_search function return the key of the value when the search is successful (key of first matched element) and false when the search is unsuccessful. Since an element can have a numeric key 0 which converts to false when converted into a boolean value, make sure you use === comparison operator when determining that a search was successful or not. If you wish to retrieve the keys of all matching elements instead of retreiving the key of just the first matching element, you can use array_keys function. |
|
|||||
|
Array search function is :-
The array_search() function search an array for a value and returns the key. array_search(value,array,strict); __________________________________________________ _______________ Web site design Last edited by garima; 05-29-2008 at 06:53 AM. |
![]() |
| Bookmarks |
| Webmaster Resources |
| • UK Web Hosting • UK WW SEO Tools • Free site submission • Web Directory |
| Advertisement |
![]() |
| Site Of the Month |
![]() Nominate site of the month |
|
|
| UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons. |
| Subscribe to our feeds |