我有一個這樣的句子,和一些鍵的數組。從鍵數組中搜索字符串。 PHP
$string = "I have a sentence like this, and array of some keys"
$keys = ['have', 'like', 'some']
我需要一個合成陣列這將檢查該串中的單詞,並收集隨後是特定鍵,如在上面的句子
have => a sentence
like => this, and array of
some => keys
即
$result = ['have'=> 'a sentence', 'like' => 'this, and array of', 'some' => 'keys'];
字符串
我可以提供的唯一提示是我一直在用遞歸進行嘗試,並且已經成功地達到了80%,其餘的都是相當的錯誤;
我這個嘗試是代碼 其中$preps
是關鍵,$query
是stirng值
public function sep($preps, $query) {
foreach ($preps as $prep) {
@list($rval, $lval) = explode(" $prep ", $query);
if($lval) {
$this->sep[$prep] = $lval;
$this->sep($preps, $lval);
$this->sep($preps, $rval);
}
}
}
然後去找它。 (你有問題嗎?) – Rizier123
最新問題? – bemontibeller
你必須指定應該發生什麼,如果一個關鍵字在字符串中多於一次?或不存在?!這個發生器背後的邏輯是什麼? –