2
<?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_arrayPHP 类似的函数?

4

1 回答 1

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;
}
于 2012-12-11T05:33:54.077 回答