2012-12-11 43 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