将 PHP 数组值与字符串的一部分进行比较
Compare PHP array value to PART of a string
我正在尝试自动筛选我的在线银行对帐单。这是我需要的一个简单示例。
我有一系列餐厅,我根据这些餐厅对我的信用卡报表进行排序:
$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";
我试过使用 array_search and in_array,但它们似乎与我需要的相反,或者它们需要 完全匹配 ,如下例所示,但这不是我需要的:
$result = array_search($string, array_column($restaurants, 'vendor'));
return $restaurants[$result]['type'];
// returns "default" because "McDonald's Restaurants Incorporated" != "mcdonald"
我希望能够将数组值 "mcdonald" 与包含该块的任何字符串匹配,然后 return 为它键入 "fastfood"。不用担心处理多次出现。
我专注于问题的 in_array 部分。编辑它以改用 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.
}
}
你需要综合考虑 - a search-in-string method, and for it to be case insensitive.
您可以通过以下方式完成此操作:
/**
* 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
是 "McDonald's Restaurants Incorporated",那么 $type
将是 "fastfood"。上面假设数组中的第一个实例是您的默认值 return 如果指定值的 none 匹配。
出于方便,我只是将其构建为匿名 function/closure,我通常会干净地封装一些我只打算 运行 一次的东西。但它在您的应用程序中作为命名函数可能更清晰。
我通过使用 array_map and array_filter 采取了不同的(功能性)方法。由于使用内置函数,它相当紧凑,并且可以完成工作。
// 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));
我正在尝试自动筛选我的在线银行对帐单。这是我需要的一个简单示例。
我有一系列餐厅,我根据这些餐厅对我的信用卡报表进行排序:
$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";
我试过使用 array_search and in_array,但它们似乎与我需要的相反,或者它们需要 完全匹配 ,如下例所示,但这不是我需要的:
$result = array_search($string, array_column($restaurants, 'vendor'));
return $restaurants[$result]['type'];
// returns "default" because "McDonald's Restaurants Incorporated" != "mcdonald"
我希望能够将数组值 "mcdonald" 与包含该块的任何字符串匹配,然后 return 为它键入 "fastfood"。不用担心处理多次出现。
我专注于问题的 in_array 部分。编辑它以改用 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.
}
}
你需要综合考虑 - a search-in-string method, and for it to be case insensitive.
您可以通过以下方式完成此操作:
/**
* 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
是 "McDonald's Restaurants Incorporated",那么 $type
将是 "fastfood"。上面假设数组中的第一个实例是您的默认值 return 如果指定值的 none 匹配。
出于方便,我只是将其构建为匿名 function/closure,我通常会干净地封装一些我只打算 运行 一次的东西。但它在您的应用程序中作为命名函数可能更清晰。
我通过使用 array_map and array_filter 采取了不同的(功能性)方法。由于使用内置函数,它相当紧凑,并且可以完成工作。
// 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));