我有这个数组:
Array
(
[0] => CMS_ADMIN
[1] => CMS_ACADEMIC
[2] => CMS_FINANCE
[3] => HRM_HR
[4] => SRM_D
[5] => SRM_CNC
[6] => SRM_TM
[7] => SRM_DE
)
我想通过使用提供的单词搜索数组值来获取数组。假设我只是提供'CMS'字样,那么我将如何得到[0] => CMS_ADMIN,[1] => CMS_ACADEMIC,[2] => CMS_FINANCE分配给新阵列。请帮帮....
答案 0 :(得分:1)
使用如下所示的函数:
function array_filter_prefix($array, $prefix) {
$result = array();
foreach ($array as $value) {
if (strpos($value, $prefix) === 0) {
$result[] = $value;
}
}
return $result;
}
给定输入数组$test
,然后可以执行此操作以获得结果:
$result = array_filter_prefix($test, 'CMS');
print_r($result);
答案 1 :(得分:1)
PHP 5.3中的击球手风格
$prefix = "CMS";
$new = array_filter($array, function ($str) use ($prefix) {
return (strpos($str, $prefix) === 0);
});