2012-12-11 44 views
1
<?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中的一些功能?C函数像PHP in_array

+1

http://stackoverflow.com/questions/7466432/search-array-for-string –

+1

不,你必须实现它自己:) btw,'in_array()'是区分大小写的,所以'in_array('mac',$ s)'将会是'false'。 –

回答

5

都能跟得上,但可以实现这样的

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; 
} 
+0

对于OP问题不是一个好的解决方案 – StaticVariable