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']
];
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, becausec
andd
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: