将PHP数组值与字符串的一部分进行比较
我正在尝试自动筛选我的网上银行对账单。这里有一个我需要的小例子 我有一系列的餐厅,我根据这些餐厅对信用卡账单进行排序:将PHP数组值与字符串的一部分进行比较,php,arrays,substring,Php,Arrays,Substring,我正在尝试自动筛选我的网上银行对账单。这里有一个我需要的小例子 我有一系列的餐厅,我根据这些餐厅对信用卡账单进行排序: $restaurants = array( array("vendor" => "default", "type" => "default" ), array("vendor" => "dunkin", "type" => "pastry" ), array("ven
$restaurants = array(
array("vendor" => "default",
"type" => "default"
),
array("vendor" => "dunkin",
"type" => "pastry"
),
array("vendor" => "mcdonald",
"type" => "fastfood"
),
array("vendor" => "olive",
"type" => "italian"
)
);
语句条目本身可以是一个描述性字符串:
$string = "McDonald's Restaurants Incorporated";
我尝试过使用and,但它们似乎与我需要的正好相反,或者它们需要一个精确匹配,如下面的示例所示,但这不是我需要的:
$result = array_search($string, array_column($restaurants, 'vendor'));
return $restaurants[$result]['type'];
// returns "default" because "McDonald's Restaurants Incorporated" != "mcdonald"
我希望能够将数组值“mcdonald”与包含该数据块的任何字符串相匹配,然后为其返回类型“fastfood”。不要担心处理多次出现的问题。我专注于问题的数组部分。编辑此文件以改为使用strpos 试试这个:
foreach($restaurants as $restaurant)
{
if(strpos($restaurant['vendor'], $string) !== FALSE)
{
return $restaurant['type']; //Or add to an array/do whatever you want with this value.
}
}
你需要一个东西的组合- 您可以通过以下方式完成此任务:
/**
* Perform a string-in-string match case insensitively
* @param string $string
* @param array $restaurants
* @return string|false
*/
function findRoughly($string, $restaurants)
{
$out = false;
foreach ($restaurants as $restaurant) {
// Set up the default value
if ($restaurant['type'] == 'default' && !$out) {
$out = $restaurant['type'];
// Stop this repetition only
continue;
}
// Look for a match
if (stripos($string, $restaurant['vendor']) !== false) {
$out = $restaurant['type'];
// Match found, stop looking
break;
}
}
return $out;
}
并像这样使用它:
$result = findRoughly("McDonald's", $restaurants);
我认为PHP中没有一个函数可以像您希望的那样干净地处理这个问题。但您可以快速启动一个函数,在数组中循环查找匹配项:
$type = call_user_func( function( $restaurants, $string ) {
foreach ( $restaurants as $restaurant ) {
if ( stripos( $string, $restaurant['vendor'] ) !== FALSE ) {
return $restaurant['type'];
}
}
return $restaurant[0]['type'];
}, $restaurants, $string );
如果$string
是“麦当劳餐厅公司”,那么$type
将是“快餐”。上面假设,如果指定的值都不匹配,那么数组中的第一个实例就是默认返回值
出于方便,我只是将其构建为一个匿名函数/闭包,我通常会将我只打算运行一次的东西干净地封装起来。但在您的应用程序中,作为一个命名函数,它可能更简洁。我采用了一种不同的(功能性)方法,使用and。由于使用了内置函数,它相当紧凑,并且可以完成任务
// Anonymous function which gets passed to array_map as a callback
// Checks whether or not the vendor is in the $key_string (your restaurant)
$cmp = function ($array) use ($key_string) {
if (stristr($key_string, $array['vendor'])) {
return $array['type'];
}
return "default";
};
function validResult($item) {
return isset($item) && gettype($item)=="string";
}
$key_string = "McDonald's Restaurants Incorporated";
$results = array_map($cmp, $restaurants);
$results = array_pop(array_filter($results, validResult));
in_array()的第二个参数不能是字符串。工作完全正确!感谢您提供的示例代码,非常感谢。这可以更有效地完成。为什么要使用临时变量$out?为什么不在找到$restaurant['type']后立即返回它,并且在循环外有一个默认的return语句?这会将函数缩短到9行,不计算注释。Hi@blasko,临时变量用于处理默认值可能不在数组开头,但可能在数组中间或结尾的情况。如果未找到匹配项或默认值,则返回false