0

我有这个数组:

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 分配给新数组。请帮忙....

4

2 回答 2

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);

键盘在这里

于 2012-05-11T03:36:27.117 回答
1

PHP 5.3 中的击球手风格

$prefix = "CMS";

$new = array_filter($array, function ($str) use ($prefix) {
    return (strpos($str, $prefix) === 0);
});
于 2012-05-11T03:41:41.480 回答