2016-10-03 58 views
-1

我不想將Word禁用系統創建爲技術支持腳本。數組中的Json數組foreach

但我無法想象如何設計json和foreach。

計劃:

"banned_words": [ 
    { 
     word, word2, word3, word4 
     message: why we banned this words 
    } 
    { 
     help, mail, register, email 
     message: we know whats the problem, be patient 
    } 
    ] 

如何JSON設計應該是,我怎麼能在他們的foreach?

我正在查看strpos()函數的票務信息。

回答

0

你在那裏有沒有有效的JSON。 (我假設你已經知道這一點)。要將你已有的東西變成有效的JSON,你需要改變一些東西。

  • 引用的所有鑰匙,並與"字符串值。

  • ,爲您在banned_words中每個對象的單詞列表,你需要 它們放在一個數組[]併爲它們分配的關鍵。

你應該JSON落得這樣的:

{"banned_words": [ 
    { 
     "words": ["word", "word2", "word3", "word4"], 
     "message": "why we banned this words" 
    }, 
    { 
     "words": ["help", "mail", "register", "email"], 
     "message": "we know whats the problem, be patient" 
    } 
    ] 
} 

後你有JSON,你可以遍歷單詞的列表及其相應的消息是這樣的:

$word_lists = json_decode($json); 

foreach ($word_lists->banned_words as $set) { 
    $message = $set->message; 
    foreach ($set->words as $word) { 
     // do something with the word 
    } 
} 

(如何從這一點實際實現禁止系統這個詞超出了Stack Overflow答案的範圍。)

+0

我做到了5 mi nutes ago,同樣的json,謝謝你的回答。真正! –