PHP comparing or searching a array of two different arrays

64 views Asked by At

I will compare the two arrays. $db is the first array and $tr is the second array. $tr be compared with$db. if the value $tr in the $db. then $result contains value of $tr:

The First Array is $db

$db = [
        ['a','d'],
        ['a','e','f'],
        ['g'],
        ['b','e','d'],
        ['a','d','c','e'],
        ['d','g'],
        ['c','a','h','e','f','h'],
        ['g','e','h'],
        ['d','f','b','h','g']
    ];

The second array is $tr

$tr = [
        ['a','b'],
        ['a','c'], //in
        ['a','d'], //in
        ['b','c'],
        ['b','d'], //in
        ['c','d'],
    ];

if $db and $tr than it produces

will have the results $result like this:

$result = [
        ['a','c'],
        ['a','d'],
        ['b','d']
    ];
2

There are 2 answers

0
GolezTrol On BEST ANSWER

Merry Christmas!

I don't think PHP has a standard function to solve this (although I may have missed the one for you). There is a plethora of array functions, but most of them don't work on nested arrays, or only perform a specific task.

But it's usually easy to write a function yourself, leveraging, if you can, the built-in functions.

The function below iterates over every item in $tr. For each item it iterates over the items of $dm. It then uses array_diff to get a list of items missing in the dm item. If that list is empty, it means all items from the first array are contained in the second array and the first array should be in the result.

I noticed this function also returns ['c', 'd'] in the result, because c and d are both contained in one of the arrays of $dm. So maybe you missed that one, or I didn't understand the specs correctly. Anyway, this should get you started at least:

<?php

$db = [
        ['a','d'],
        ['a','e','f'],
        ['g'],
        ['b','e','d'],
        ['a','d','c','e'],
        ['d','g'],
        ['c','a','h','e','f','h'],
        ['g','e','h'],
        ['d','f','b','h','g']
    ];

$tr = [
        ['a','b'],
        ['a','c'], //in
        ['a','d'], //in
        ['b','c'],
        ['b','d'], //in
        ['c','d'],
    ];


function nested_array_intersect($source, $comparewith) {
  $result = array();

  foreach ($source as $sItem) {

    foreach ($comparewith as $cwItem) {

      $missingItems = array_diff($sItem, $cwItem);

      if (count($missingItems) === 0) {

        $result[] = $sItem;
        break;

      }
    }
  }
  return $result;
}

$result = nested_array_intersect($tr, $db);

print_r($result);
0
Ace Xu On
function compare($db, $tr)
{
    $result = [];

    foreach ($db as $val) {
        $dbformat[] = implode("", $val);
    }

    foreach ($tr as $val) {
        $pattern = "";
        foreach($val as $one) {
            $pattern .= $one.".*?";
        }
        foreach ($dbformat as $value) {
            $countMatch = preg_match("/".$pattern."/", $value);
            if($countMatch) {
                $result[] = $val;
                break;
            }
        }
    }

    return $result;
}