-1

我有一个数组,例如这个例子:

Array
(
    [0] => cinema
    [1] => school
    [2] => college
    [3] => social
    [4] => cinema
    [5] => School
    [6] => COllEGE
    [7] => Ccccccc
)

我只想要从“C”或“S”开始的整个单词一次,单词中的重复字符是允许的,无论它们是大写还是小写

示例输出:

cinema
college
ccccccc
4

2 回答 2

1

使用array_filter简单的过滤器(正则表达式或$val[0] == "c"例如)和array_unique

这是一个示例(未测试):

$data = array(...data...);

function check_value($val) {
  return preg_match('/^c/i', $val);
}

$output = array_unique(array_filter($data, 'check_value'));
于 2012-07-03T16:10:35.317 回答
0

数组函数的php手册列表和字符串函数的列表可能有用:

<?php
  $arr =  array ( 'cinema', 'school', 'college', 'social', 'cinema', 'School', 'COllEGE' );
  $massaged_array = massage($arr);
  $result = array_count_values($massaged_array);
  foreach ($result as $key => $value) {
    if (substr_compare($key, 'C', 0, 1) || substr_compare($key, 'S', 0, 1)){
      echo $key;
    }
  }    

  function massage ($arr) {
    $result = array();
    foreach ($arr as $value) {
      $result[] = strtolower($value);
    }
    return $result;
  }
于 2012-07-03T16:08:12.150 回答