2015-10-05 69 views
-5

閱讀我有這樣一個數組: -PHP:檢查並從陣列

$arr = array(
    "Sci-Fi", 
    "Action" 
); 

如何檢查是否存在這個數組中Action,以及它如何轉換爲(Sci-Fi,Action)

Thankx

+2

使用php在線文檔。你可以使用[in-array](http://php.net/manual/ro/function.in-array.php)和[implode](http://php.net/manual/ro/function.implode。 php) – Filip

回答

1

在你的問題$ arr是數組。首先我們將檢查'Action'是否在數組中。

if(in_array('Action',$arr)){ 
     //in_array checks if 'Action' is in the array 
     echo "Action is in the array!"; 
    } 

現在我們需要「implode」數組將它變成一個字符串。

echo "(" . implode(',', $arr) . ")"; 
    //implode glues the parts of your array together into a string using the supplied "glue", in this case a comma. 

php.net擁有這一切的記載,但我知道這很難,只是知道什麼是函數名稱,尤其是神祕的東西一樣破滅。當你學習時,很高興得到一個友好的答案。

+1

請解釋你的解決方案。 –

+0

我通常會發布答案,然後編輯它以添加更好的解釋。如果我先把它寫完,有人會用簡單的答案來打敗我。如此快速回答OP,然後解釋組織的優點。這是衆多可怕的行爲之一,鼓勵可怕的堆棧溢出策略。不要討厭玩家,討厭遊戲。 – RightClick

+0

我什麼都不討厭。我不知道每一個SO用戶的習慣,我對社區的善意表現。這只是一個禮貌的提醒,不是個人的咆哮。 –