Webmaster Forum
Go Back   Webmaster Forums UK SEO SEM Webmaster Community Forum - UKWW > Web Design and Website Development > Programming > PHP Forum
Register FAQ Members List Downloads Calendar Today's Posts Webmaster Resources Webmaster Blogs

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-10-2008, 05:00 PM
dman_2007
Guest
 
Posts: n/a
iTrader: / %
Default Tip : Searching in array in php

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 />';
  } 
?>
the above code produces following output :

Quote:
1 is present in the array
0 is present in the array (when not passing third argument)
0 is not present in the array (when passing third argument)
JOE is not present in the array
in my next post i will discuss the second method.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 05:25 PM
dman_2007
Guest
 
Posts: n/a
iTrader: / %
Default

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.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 05-28-2008, 06:46 AM
garima's Avatar
Junior Member
 
Join Date: May 2008
Posts: 17
iTrader: 0 / 0%
Thanks: 0
Thanked 0 Times in 0 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
garima is on a distinguished road
Default

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 05:53 AM.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 08-27-2008, 08:22 AM
Junior Member
 
Join Date: Aug 2008
Posts: 3
iTrader: 0 / 0%
Thanks: 0
Thanked 0 Times in 0 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
dyamni is on a distinguished road
Default

garima can you let me know with example of this function.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #5 (permalink)  
Old Yesterday, 12:12 PM
Dimitar Christoff's Avatar
Member
 
Join Date: Jul 2008
Location: Lodnon, England
Posts: 35
iTrader: 0 / 0%
Thanks: 7
Thanked 0 Times in 0 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Dimitar Christoff is on a distinguished road
Default

PHP Code:
<?php
$array 
= array(=> 'blue'=> 'red'=> 'green'=> 'red');

$key array_search('green'$array); // $key = 2;
$key array_search('red'$array);   // $key = 1;
?>
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
Reply

Bookmarks

Webmaster Resources
UK WW SEO Tools
Find UK Hosts
 
The Forum Rules
Forum Rules - MUST READ
 
Site Of the Month
BizzFace
Nominate site of the month
 
Tag Cloud
adf apple iphone apple iphone 3g article database article site article writer bidding directory bid directory bid for position contents data entry free social hosting free space google google analytical tool graphics design joomla link exchange mysql partners phpmyadmin programming research site dump social web space wanted uk web development web directory web hosting webmasters web space for free wordpress

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump



All times are GMT. The time now is 07:05 AM.

UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons.
Subscribe to our feeds   Subscribe to our feeds

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151