2015-04-01 21 views
0

夥計們,我可能有一個非常簡單的問題,但我不知道如何製作它。PHP - 使用逗號將變量文本轉換爲數組變量並在IF語句中生成後

如何將下列文本轉換爲變量數組。

$NotParsedString = "123456,654987,789456,321465"; 

foreach($results['data'] as $item){ 

    $PostID = $item['id']; 
    if($PostID != $AnyIDfromNotParsedString){ 
     echo 'Show this thing'; 
    } 
} 

只要我想從$NotParsedString刪除所有的逗號,讓他們作爲單獨的ID,我可以比較$PostID如果$PostID不與任何theese ID到echo 'Show this thing';

我希望你能我想要做什麼的整個想法。

我想要一些像if $PostID is not equal to 123456if $PostID is not equal to 654987等到剩下的結束。

你能幫我出去嗎?

在此先感謝!

+1

1. http://php.net/manual/en/function.explode.php 2. http://php.net /manual/en/function.in-array.php – Sammitch 2015-04-01 22:40:21

+0

你的預期輸出是什麼? – Rizier123 2015-04-01 22:50:58

+0

我已更新我的問題 – 2015-04-01 22:54:01

回答

2

只要使用in_array()explode()這樣的組合:

if(!in_array($PostID, explode(",", $NotParsedString))){ 
+0

我已經標記爲答案作爲解決問題的答案,但是您有一個incorect代碼。 if(!in_array($ PostID,explode(「,」,$ NotParsedString)){'must be'if(!in_array($ PostID,explode(「,」,$ NotParsedString))){'Thanks :) – 2015-04-01 23:13:29

+0

@ TonyStark啊是忘了一個支架 – Rizier123 2015-04-01 23:14:05

1

只需使用explode()函數。

$NotParsedString = "123456,654987,789456,321465"; 
$ids = explode(',', $NotParsedString); 

$ id將如下。

Array 
(
    [0] => 123456 
    [1] => 654987 
    [2] => 789456 
    [3] => 321465 
) 
+1

但這隻會刪除逗號,它不會從左ID創建一個數組? – 2015-04-01 22:47:34