<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>
C中是否有一些函数与PHP中的in_array
类似?
答案 0 :(得分:6)
不是,但您可以像这样实施
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;
}