★ wanayoo — archive 1999 http://www.php.net/manual/ja/function.in-array.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Arrays
Quick Reference
English version of this pageGerman version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Arrays
* array
* array_count_values
* array_diff
* array_flip
* array_intersect
* array_keys
* array_merge
* array_merge_recursive
* array_multisort
* array_pad
* array_pop
* array_push
* array_rand
* array_reverse
* array_shift
* array_slice
* array_splice
* array_unshift
* array_values
* array_walk
* arsort
* asort
* compact
* count
* current
* each
* end
* extract
* in_array
* key
* krsort
* ksort
* list
* next
* pos
* prev
* range
* reset
* rsort
* shuffle
* sizeof
* sort
* uasort
* uksort
* usort
Manual: in_array
View the source code for this pageSearch the site



Previous page
 extract
 Updated
Sat, 12 Aug 2000
key 
Next page


in_array

(PHP4 )

in_array -- 配列に値がある場合にtrueを返す

説明

bool in_array (mixed needle, array haystack)

haystackneedleを検索し、配列にそれがあった場合にtrueを 返します。それ以外の場合は、falseを返します。

例 1. in_array()の例


$os = array ("Mac", "NT", "Irix", "Linux");
if (in_array ("Irix", $os))
    print "Got Irix";
      

注意: この関数は、PHP 4.0で追加されました。


User Contributed Notes: in_array


geebee@start.com.au
15-Sep-1999 10:20
you have a haystack;

you have a needle you want to find;

you ask your buddy in_array if he knows if your needle is in the haystack;

he looks in the haystack, turns to you and answers "yes it is"

so you look at the haystack which is several miles high, wide, and deep and ask him "where in the haystack ?"

and he says ".... you dont need to know, try asking someone else"




Espen.Lyngaas@colorline.no
03-Dec-1999 09:43
If you haven't gotten around to installing PHP4 yet, you could do:
(Not very optimized :) It could easily return the location of $needle instead
of just wheter or not it found it (uncomment).

<pre>
function in_array($needle,$haystack) {
$found = false;
// $found = -1;
for($ptr=0;$ptr<count($haystack);$ptr++) {
if($haystack[$ptr] == $needle) {
$found = true;
// $found = $ptr;
}
}
return $found;
}
</pre>



ivo@ibuildings.nl
20-Jan-2000 10:14
A more optimised version for php3 than the one above:

<pre>
function in_array($needle,$haystack)
{
for($i=0;$i<count($haystack) && $haystack[$i] !=$needle;$i++);

return ($i!=count($haystack));
}
</pre>



nick@profile.com
12-Feb-2000 12:19
in response to geebee@start.com.au's where's the needle in the haystack,
check out http://www.php.net/manual/function.each.php3

now you could do something like,
<PRE>
$array = array("one", "two", "three", "etc");
while ($i = each($array)) {
if ($i["value"] == "three") {
echo "three was found in the haystack's element " , $i["key"];
}
}
</PRE>



kballou@wt.net
21-Feb-2000 07:02
It appears that if you have an array of numbers, one element of which is zero, and you pass in_array a string to compare to the values in that array, all string values will equate with the "0" element.

For instance:
$arr1 = array(0, 1, 2, 3, 4);
in_array("asdf", $arr1) == True

if you iterate through the array manually, "asdf" will equate with the "0" element.

If you write your own in_array, and compare each element to the needle using strcmp, it appears to work properly.

i.e.:
function in_str_array($needle, $haystack) {

reset($haystack);
while( list($key, $val) = each($haystack) ) {
if( !strcmp($val, $needle) ) {
return true;
}
}
return false;
}



kballou@wt.net
21-Feb-2000 07:04
sorry!
<pre>function in_str_array($needle, $haystack) {

reset($haystack);
while( list($key, $val) = each($haystack) ) {
if( !strcmp($val, $needle) ) {
return true;
}
}
return false;
}
</pre>



spstrong@uswest.net
20-Apr-2000 05:44
What if the array has its own key? Will this check the key and values?



tip@linuxfan.com
27-Apr-2000 07:20
What if the keys aren't sequential? This will work if they don't have sequential keys.

<pre>
function in_array($needle,$haystack)
{
$found=false;
while(list($counter,$haystack_rec)=each($haystack)){
if($needle==$haystack_rec){
$found=true;
}
}
return $found;
}
</pre>



&quot;krogoth2&quot;.&quot;@&quot;.&quot;softhome.net&quot;
28-Apr-2000 07:25
nick@profile.com: this works, but i looked for this function because i didn't want to use the for loop.


brunofr@ioda.net
21-May-2000 08:54
Well seems to not work on multiple arrays


junk@newtradesmen.com
19-Jul-2000 01:31
$a = array("foo", "bar");


if(in_array("bar", $a))
print current($a);
// I think this should print 1

IMO, since in array returns bool, and we really need an int which is where the first occurance of "bar" appears in the array.




timdown@hotmail.com
26-Jul-2000 02:35
For those of you who are still using PHP 3 and those who want in_array to return the key for the value being searched for, try the following alternative function:
<pre>
function is_in_array($needle,$haystack) {

$needleFound = false;

$returnValue = false;

reset($haystack);

while ((list($key,$value) = each($haystack)) && !$needleFound) {

if ($needle == $value) {

$needleFound = true;

$returnValue = $key;

}

}

return $returnValue;

}

</pre>



 About Notes


Previous page
 extract
 Updated
Sat, 12 Aug 2000
key 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.