<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>
C 中是否有一些与in_array
PHP 类似的函数?
不,但你可以这样实现
typedef int (*cmpfunc)(void *, void *);
int in_array(void *array[], int size, void *lookfor, cmpfunc cmp)
{
int i;
for (i = 0; i < size; i++)
if (cmp(lookfor, array[i]) == 0)
return 1;
return 0;
}
int main()
{
char *str[] = {"this is test", "a", "b", "c", "d"};
if (in_array(str, 5, "c", strcmp))
printf("yes\n");
else
printf("no\n");
return 0;
}