简体   繁体   English

检查数组 1 的值是否存在于数组 2 的键中 - PHP

[英]Check if the value of array 1 is present in the key of array 2 - PHP

I have two array:我有两个数组:

$arr1 = Array
(
    [0] => Apple
    [1] => Ball
    [2] => whale
    [3] => Dog
    [4] => cat
)

and

$arr2 = Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [Red apple] => 27
            [tball] => 28
            [cat] => 29
            [Dog] => 30
            [blue ball] => 31
        )
    ]
)

Here, I want to check if the value of 1st array matches ('% like %') to any key of second array.在这里,我想检查第一个数组的值是否与第二个数组的任何键匹配('% like %') I would like to explain it with respect to this example.我想就这个例子来解释一下。

Here, you can see 1st value of $arr1 is Apple , now you can see Red apple on arr2.在这里,您可以看到$arr1第一个值是Apple ,现在您可以在 arr2 上看到Red apple This means this should be considered as match.这意味着这应该被视为匹配。 However, Ball should not be matched with tball instead Ball should match to blue ball .但是, Ball不应与tball匹配,而Ball应与blue ball匹配。

If it does find the match then I want the value to be stored in $result array, if there is no matched value the particular index of $result array must be empty.如果它确实找到匹配,那么我想被存储在价值$result数组,如果没有匹配值的特定索引$result数组必须是空的。 In this case I want the exact result as:在这种情况下,我想要确切的结果:

$result= Array
(
    [0] => 27
    [1] => 31
    [2] => 
    [3] => 30
    [4] => 29
)

sandboxphp 沙盒php

hello just try regular exp and try this你好,试试普通的 exp 然后试试这个

   $arr1 = Array('Apple', 'Ball', 'whale', 'Dog', 'cat');
    $arr2=Array('Red apple' => 27, 'tball' => 28, 'cat' => 29, 'Dog' => 30, 'blue ball' => 31);
    $array=array();
    foreach ($arr1 as $arr) {
        $flag=false;
        foreach ($arr2 as $a=>$ar) {
            $search = '\b( ?'.$arr.')';
            if(preg_match("/{$search}/i", $a)) {
                array_push($array,$ar);
                $flag=true;
            }
        }
        if (!$flag) {
            array_push($array,'');
        }
    }
    var_dump($array); die;

Looking at your link the real arrays look much larger and I assume a two way match can be possible.查看您的链接,实际数组看起来要大得多,我认为可以进行双向匹配。
Because of that I made a slight difference to the output the code to group on category.因此,我对要按类别分组的代码的输出略有不同。
I'm not sure what to do if there is a match of two, what number should be retrieved?如果有两个匹配项,我不确定该怎么办,应该检索哪个数字?
I also use preg_grep which uses regex on arrays instead of looping the array.我还使用了 preg_grep,它在数组上使用正则表达式而不是循环数组。
And preg_quote to make sure regex characters does not interfere with the regex.和 preg_quote 以确保正则表达式字符不会干扰正则表达式。

$arr1 = Array('Apple', 'Ball', 'whale', 'Dog', 'cat');
$arr2 = Array('Red apple' => 27, 'tball' => 28, 'cat' => 29, 'Dog' => 30, 'blue ball' => 31, 'dog food' => 25, 'green apple' => 111);
$keys = array_keys($arr2);

foreach($arr1 as $find){
    $matches = preg_grep("/\b" . preg_quote($find) . "/i", $keys);
    foreach($matches as $key => $word){
        $result[$find][] = $arr2[$word];
    }
}

var_dump($result);

Output of this:输出:

array(4) {
  ["Apple"]=>
  array(2) {
    [0]=>
    int(27)
    [1]=>
    int(111)
  }
  ["Ball"]=>
  array(1) {
    [0]=>
    int(31)
  }
  ["Dog"]=>
  array(2) {
    [0]=>
    int(30)
    [1]=>
    int(25)
  }
  ["cat"]=>
  array(1) {
    [0]=>
    int(29)
  }
}

https://3v4l.org/es2Gt https://3v4l.org/es2Gt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM