1 回答
TA贡献1833条经验 获得超4个赞
这可能是您正在寻找的:
<?php
$searchTerms = ['mobile', 'Mobile Legend', 'Mobile', 'mobile legend'];
$data = [
[
'id' => 1,
'Name' => "Mobile Game",
'CategoryName' => "Mobile Legend"
],
[
'id' => 2,
'Name' => "Laptop Game",
'CategoryName' => "Mobile"
],
[
'id' => 3,
'Name' => "Something",
'CategoryName' => "Console"
]
];
$output = [];
foreach ($searchTerms as $searchTerm) {
$pattern = sprintf('/%s/i', preg_quote($searchTerm));
array_walk($data, function($entry, $index) use ($pattern, &$output) {
if (!array_key_exists($index, $output)
&& (preg_match($pattern, $entry['Name'])
|| preg_match($pattern, $entry['CategoryName']))) {
$output[$index] = $entry;
}
});
}
print_r($output);
明显的输出是:
Array
(
[0] => Array
(
[id] => 1
[Name] => Mobile Game
[CategoryName] => Mobile Legend
)
[1] => Array
(
[id] => 2
[Name] => Laptop Game
[CategoryName] => Mobile
)
)
- 1 回答
- 0 关注
- 120 浏览
添加回答
举报