2013-10-21 61 views
2

我有定義以下數組:交叉口PHP

array(
    'name'=>'Blue', 
    'age'=>'0', 
    'skin'=>array(
     'White Skin','Tanned Skin' 
    ), 
    'eye'=>array(
     'Black','Brown','Honey' 
    ), 
    'personality'=>array(
     'Intelligent','Warm','Trustworthy','Sweet' 
    ), 
    'ocassion'=>array(
     'Every day wear','Celebrations','Restaurant Dinner','Feasts','Visiting friends' 
    ), 
    'hair'=>'All Colors', 
    'style'=>array(
     'Loved to be admired','Center of attention' 
    ), 
    'description'=>'Blue lens are perfect for any..' 
); 

,我試圖找到匹配的數量,從一個HTML表單到該陣列。從HTML形式的可能的返回,在陣列格式是:

Array 
(
[age] => 16 
[skin] => Tanned Skin 
[eye] => Brown 
[personality] => Array 
    (
     [0] => Intelligent 
     [1] => Warm 
     [2] => Trustworthy 
    ) 

[ocassion] => Weddings 
[hair] => Dark Brown 
[style] => Array 
    (
     [0] => Style Queen 
     [1] => Testing val 
    ) 

) 

我曾嘗試迭代低谷第一陣列的每個鍵,但未能達到我想要的,也是我一直在使用的函數試圖array_intersect_assoc($stack,$search)但它似乎不會找到完全匹配,因爲$ search數組(第二個示例)具有一些鍵類型爲string的key =>值對,並且它無法將任何出現匹配到第一個數組中,因爲該值實際上是數組,而不是一個字符串。

有人可以指出我的想法,或可以讓我知道什麼是最好在這裏做?

我在過去的3個小時裏嘗試了很多東西,但沒有成功。

+0

什麼**確實**你認爲匹配? – Smuuf

+0

例如在定義的數組中,我有'''eye'=>數組('Black','Brown','Honey')'',並且從HTML獲得''[eye] => Brown''。我認爲HTML的響應正在被定義的數組中找到,所以它是匹配的 – roshkattu

+0

另外,如果我從** ** **或** ** **的HTML中獲取數組,我認爲每個值都匹配可以在_personality_或_style_數組鍵下找到已定義數組中的數組。 – roshkattu

回答

1

好的,那麼怎麼樣。 源數據:

$demands = array(
    'name'=>'Blue', 
    'age'=>'0', 
    'skin'=>array(
     'White Skin','Tanned Skin' 
    ), 
    'eye'=>array(
     'Black','Brown','Honey' 
    ), 
    'personality'=>array(
     'Intelligent','Warm','Trustworthy','Sweet' 
    ), 
    'ocassion'=>array(
     'Every day wear','Celebrations','Restaurant Dinner','Feasts','Visiting friends' 
    ), 
    'hair'=>'All Colors', 
    'style'=>array(
     'Loved to be admired','Center of attention' 
    ), 
    'description'=>'Blue lens are perfect for any..' 
); 

$possible_match = array(
    'age'=>'16', 
    'skin'=>'Tanned Skin', 
    'eye'=>'Brown', 
    'personality'=>array(
     'Intelligent','Warm','Trustworthy' 
    ), 
    'ocassion'=>array(
     'Weddings' 
    ), 
    'hair'=>'Dark Brown', 
    'style'=>array(
     'Style Queen','Testing value' 
    ) 
); 

和匹配,使算法:

$result = array(); 
$count_matches = 0; 

// Go through all the demands 
foreach ($demands as $key => $value){ 

    // If there's a matching key in the possible match array 
    if (isset($possible_match[$key])){ 
     // If there are more demanded values 
     if (is_array($value)){ 
      // Let all demanded values be lowercase 
      $value = array_map('strtolower', $value); 
      // If there are more possible matching values 
      if (is_array($possible_match[$key])){ 
       // Let all possibly matching values be lowercase, too 
       $possible_match[$key] = array_map('strtolower', $possible_match[$key]); 
       // And then do the intersect. 
       $intersect = array_intersect($value, $possible_match[$key]); 
       if ($intersect){ 
        // If that intersect is not empty, add that to the resulting array 
        $result[$key] = $intersect; 
        $count_matches += count($intersect); 
       }; 
      } else { 
       // If there's only one possible matching value, search that 
       // value in the demaned array 
       if (in_array(strtolower($possible_match[$key]), $value, true)){ 
        // And add it to the results 
        $result[$key][] = strtolower($possible_match[$key]); 
        $count_matches++; 
       } 
      } 
     } else { 
      if (is_array($possible_match[$key])){ 
       // If there are more possible matching values but the demand is a string, 
       // find that string in those possible values 
       $possible_match[$key] = array_map('strtolower', $possible_match[$key]); 
       if (in_array(strtolower($value), $possible_match[$key], true)){ 
        // And add it to the results 
        $result[$key] = $value; 
        $count_matches++; 
       } 
      } else { 
       // If the demanded value is only one (= it's a string and not an array) 
       // and the possible match is also a string, do a lowercase compare 
       // + if there's a word "all" in the demanded value, pass it at all times ;D 
       if (strtolower($possible_match[$key]) == strtolower($value) 
        || stripos($value, "all") !== false){ 
        // And add it to the resulting array 
        $result[$key] = strtolower($value); 
        $count_matches++; 
       } 
      } 
     } 
    } 

} 

var_dump ($result); 
var_dump ($count_matches); 

有可能是用於優化一些機會,但基本的思路應該是有:)

結果:

array (size=4) 
    'skin' => 
    array (size=1) 
     0 => string 'tanned skin' (length=11) 
    'eye' => 
    array (size=1) 
     0 => string 'brown' (length=5) 
    'personality' => 
    array (size=3) 
     0 => string 'intelligent' (length=11) 
     1 => string 'warm' (length=4) 
     2 => string 'trustworthy' (length=11) 
    'hair' => string 'all colors' (length=10) 

加上計數,如果你願意:

int 6 
+0

謝謝。它的工作原理與其應有的一樣。我已經採取了另一種方法最終與兩個foreach循環,但後來我看到了你的答案,我發現它更優化。所以我用你的。對於大拖延抱歉。 – roshkattu

+0

沒問題,我很高興我可以幫助:) – Smuuf