简体   繁体   中英

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. 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. This means this should be considered as match. However, Ball should not be matched with tball instead Ball should match to 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. In this case I want the exact result as:

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

sandboxphp

hello just try regular exp and try this

   $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.
And preg_quote to make sure regex characters does not interfere with the regex.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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