我有一个数组,例如:
Array
(
[0] => cinema
[1] => school
[2] => college
[3] => social
[4] => cinema
[5] => School
[6] => COllEGE
[7] => Ccccccc
)
我只希望只有一次从“C”或“S”开始的整个单词, 无论是大写还是小写,都允许在单词中重复字符
示例输出:
cinema
college
ccccccc
答案 0 :(得分: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'));
答案 1 :(得分:0)
php手册list of array functions和list of string functions可能有用:
<?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;
}