2017-08-07 90 views
-1

我知道這看起來像一個瘋狂的問題,但我無法找出以下情況的最佳解決方案。在一串逗號分隔的單詞(乾草堆)中搜索字符串(針)

我有一個「類別」數組,每個都有一個「搜索」字符串。這些搜索是一串逗號分隔的單詞,其作用類似strpos以匹配類別。類別陣列

$categories = [ 
    0 => [ 
    'category' => 'Foo', 
    'searches' => 'fet,cre,oho' 
    ], 
    1 => [ 
    'category' => 'Bar', 
    'searches' => 'lois,vreb,mon' 
    ], 
] 

這裏

實施例是問題。假設我有這個詞secret。如您所見,$categories中的第一個數組有searches,其中包含字符串中的cre。我遇到的問題是給出一個字符串(在這個例子中是'secret')的一個基於給定字符串的類別,其中一個用逗號分隔的單詞中的一個搜索,然後返回類別(只能有一個貓)。

結果我要找:

function findCategory($string) 
{ 
    $category = false; 

    // code to take the string, 
    // look through $categories 
    // find the first occurrence of a string position 
    // in the comma separated list of possible matches 

    return $category ? $category : 'Other'; 
} 

echo findCategory('secret'); // Foo 
echo findCategory('monster'); // Bar 
echo findCategory('cohort'); // Foo 
echo findCategory('tense'); // Other 
+4

您需要**嘗試自己編寫代碼**。在[**做更多的研究**之後](https://meta.stackoverflow.com/q/261592/1011527)如果你有問題**發佈你已經嘗試了**的**明確的解釋不工作**並提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。閱讀[如何問](http://stackoverflow.com/help/how-to-ask)一個很好的問題。請務必[參觀](http://stackoverflow.com/tour)並閱讀[this](https://meta.stackoverflow.com/q/347937/1011527)。 –

+0

這個問題有點寬泛,你有沒有嘗試過自己呢? – RiggsFolly

+0

最好的我可以通過所有類別和搜索在每個類別中的id循環,然後將每個搜索作爲正則表達式模式並將其與原始字符串進行匹配。如果有匹配,則分配類別。 – SchoolBoy

回答

2

我不知道GrumpyCruton的答案很會爲in_array()方法的工作將需要一個完整的比賽不根據需要進行部分匹配。也許類似下面就爲你工作:

function findCategory($needle, $categories) { 
    foreach($categories as $category){ 
    $searches = explode($delimiter, $category['searches']); 
    foreach ($searches as $search){ 
     if(strpos($needle, $search) !== false){ 
     return $category['category']; 
     } 
    } 
    } 
    return 'Other'; 
} 

我沒有實際測試過這一點,但我覺得應該讓你在正確的軌道上。

+0

如果迎合了「爲我做的,人羣」,他們只是不停地回來了更多 – RiggsFolly

+0

@RiggsFolly如果有人想幫助讓他們幫忙。超過90%的時間我自己動手去解決,但是我一整天都在爲此而戰。我想要一個起點。善良.. –

+0

@RiggsFolly也許,但如果我幫助某人得到他們的解決方案,那麼我不在乎。雖然,我可能應該更多地爲教育目的評論它。 –

1

如果我理解正確你的問題,那麼這個功能應該可以幫助您。

function findCategory($needle, $categories, $delim = ",") { 
    //loop through every category 
    foreach($categories as $category) { 
     //split searches string into an array of strings 
     $searches = explode($delim, $category['searches']); 
     //if needle exists in array of strings, return category. 
     if(in_array($needle, $searches)) return $category['category']; 
    } 
} 

使用案例:

findCategory($needle, $categories); 

您還可以選擇傳遞一個第三個參數作爲單詞之間的分隔符的searches

+1

你應該讓OP在寫答案之前嘗試,不是嗎? – ksjohn

+0

@ksjohn在另一次閱讀我不認爲這實際上幫助OP反正。但無論哪種方式,我總是大量評論我的答案,讓他們幫助OP學習如何做到這一點 – GrumpyCrouton

+0

你是正確的,這不,但是這種方法會@GrumpyCrouton另一個讀爲自己類似 – SchoolBoy

0
function findCategory($word) { 
    global $categories; 
    foreach ($array as $key => $val) { 
     $subarray = explode(',', $val['searches']); 
     foreach ($subarray as $str) { 
      if (strpos($word, $str) !== false) { 
       return ($val['category']); 
      } 
     } 
    } 
    return 'Other'; 
} 

我的函數獲得與類別與global數組,如果你想要的話,你可以將它包含在函數本身中,同樣的事情。 它遍歷數組,然後遍歷當前的['searches']的爆炸,並嘗試查找作爲參數傳遞的單詞中的當前單詞,如果成功,則返回相應類別的名稱。如果函數嘗試每一個字符串都沒有成功,則返回'Other'。

+0

該死的,我看到OP似乎沒有試圖解決她/他的問題,直到顯示更多的代碼,但我看起來像是一場徒勞的戰鬥。我還不如取消刪除... – ksjohn

0

試試這個功能:

<?php 

$categories = [ 
    ['category' => 'Foo', 'searches' => 'fet,cre,oho'], 
    ['category' => 'Bar', 'searches' => 'lois,vreb,mon'], 
]; 

function findCategory($string, $categories) 
{ 
    foreach($categories as $category) { 
     foreach(explode(',', $category['searches']) as $search) { 
      if (false !== strpos($string, $search)) { 
       return $category['category']; 
      } 
     } 
    } 

    return 'Other'; 
} 

echo findCategory('secret', $categories) . '<br>'; 
echo findCategory('monster', $categories) . '<br>'; 
echo findCategory('cohort', $categories) . '<br>'; 
echo findCategory('tense', $categories) . '<br>'; 

輸出:

Foo 
Bar 
Foo 
Other 
+0

如果迎合了「爲我做的,人羣」,他們只是不停地回來更多 – RiggsFolly