【问题标题】:Intersection of two multidimensional arrays in PHPPHP中两个多维数组的交集
【发布时间】:2013-10-23 00:32:39
【问题描述】:

我定义了以下数组:

   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数组(第二个示例)有一些字符串类型的键=>值对,它无法匹配第一个数组中的任何匹配项,因为该值实际上是一个数组,而不是字符串。

有人可以给我一个想法或者可以让我知道在这里做什么最好吗?

在过去的 3 个小时里,我尝试了很多东西,但都没有成功。

【问题讨论】:

  • 您认为究竟是什么?
  • 例如在定义的数组中我有 'eye'=>array('Black','Brown','Honey') 并且从 HTML 我得到 [eye] => Brown 。我认为来自 HTML 的响应被找到到定义的数组中,所以它是匹配的
  • 另外,如果我从 HTML 中得到一个数组,如 personalitystyle 我认为匹配该数组的每个值可以找到personalitystyle 数组键下定义的数组。
  • 好的,所以您需要一个由两个数组中的值组成的数组。我很笨。 :D
  • 是的。但是值应该在每个数组键下匹配。因此,对于从 HMTML 获得的每个数组键,我需要计算在定义的数组中可以找到多少个值(要么只有一个字符串,要么是一个子数组)

标签: php arrays multidimensional-array


【解决方案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

【讨论】:

  • 谢谢。它完全可以正常工作。我终于用两个 foreach 循环采取了另一种方法,但后来我看到了你的答案,我发现它更优化了。所以我用了你的。很抱歉耽搁了这么久。
猜你喜欢
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
相关资源
最近更新 更多