PHP-按字母顺序在某个字母后拼接数组

PHP-按字母顺序在某个字母后拼接数组,php,arrays,sorting,alphabetical,letter,Php,Arrays,Sorting,Alphabetical,Letter,我正在寻找一种解决方案,从排序数组中获取字母或数字后的所有单词(或数字)。即字母K之后的所有国家 $countries = array( 'Luxembourg', 'Germany', 'France', 'Spain', 'Malta', 'Portugal', 'Italy', 'Switzerland', 'Netherlands', 'Belgium', 'Norway', 'Sweden', 'Finland', 'Poland', 'Lithuania',

我正在寻找一种解决方案,从排序数组中获取字母或数字后的所有单词(或数字)。即字母K之后的所有国家

$countries = array(
'Luxembourg', 
'Germany', 
'France', 
'Spain', 
'Malta',
'Portugal', 
'Italy', 
'Switzerland', 
'Netherlands',  
'Belgium', 
'Norway', 
'Sweden', 
'Finland', 
'Poland',
'Lithuania', 
'United Kingdom', 
'Ireland', 
'Iceland',
'Hungary', 
'Greece', 
'Georgia'
);

sort($countries);
比利时、芬兰、法国、格鲁吉亚、德国、希腊、匈牙利、冰岛、爱尔兰、意大利、立陶宛、卢森堡、马耳他、荷兰、挪威

但我只想要字母K后面的国家:立陶宛、卢森堡、马耳他、荷兰、挪威


有什么想法吗?

使用
array\u filter
功能过滤掉你不想要的东西

$result = array_filter( $countries, function( $country ) {
  return strtoupper($country{0}) > "K";
});
您可以这样做:

$countries2 = array();
foreach ($countries as $country) {
    if(strtoupper($country[0]) > "K") break;
    $countries2[] = $country;
}

我终于找到了一个简单的解决方案,在任何分隔符之后拼接数组。即使不是字母或数字(如“2013年12月03日”)。只需在数组中插入所需的分隔符,然后排序,然后拼接:

//dates array:
$dates = array(
'2014_12_01_2000_Jazz_Night',
'2014_12_13_2000_Appletowns_Christmas',
'2015_01_24_2000_Jazz_Night',
'2015_02_28_2000_Irish_Folk_Night',
'2015_04_25_2000_Cajun-Swamp-Night',
'2015_06_20_2000_Appeltowns_Summer_Session'
);

date_default_timezone_set('Europe/Berlin');//if needed
$today = date(Y."_".m."_".d);//2014_12_03 for delimiter (or any other)
$dates[] = $today;//add delimiter to array
sort($dates);//sort alphabetically (with your delimiter)


$offset = array_search($today, $dates);//search position of delimiter
array_splice($dates, 0, $offset);//splice at delimiter
array_splice($dates, 0, 1);//delete delimiter
echo "<br>next date:<br>".$dates[0];//array after your unique delimiter
//日期数组:
$dates=数组(
“2014年12月01日2000日爵士之夜”,
“2014年12月13日2000年苹果城圣诞节”,
“2015年爵士之夜”,
“2015年爱尔兰民谣之夜”,
“2015年4月25日2000年卡琼沼泽之夜”,
“2015年6月20日2000年夏季会议”
);
日期默认时区设置(“欧洲/柏林”)//如果需要
$today=日期(Y.“uu.m.“uu.d”)//2014_12_03用于分隔符(或任何其他)
$dates[]=$today//向数组中添加分隔符
排序(日期)//按字母顺序排序(使用分隔符)
$offset=数组搜索($today,$dates)//分隔符的搜索位置
阵列拼接($dates,0,$offset)//分隔符处的拼接
阵列拼接($dates,0,1)//删除分隔符
回显“
下一个日期:
”$dates[0]//唯一分隔符后的数组

希望这能帮助所有想在某件事情之后拼接自然有序数组的人。

请注意,匿名函数仅在PHP 5.3+中可用。隐马尔可夫模型。。返回一个空数组吗?使用
strtoupper
并与大写字母进行比较,或者用小写字母进行比较。这样就可以了。下面的问题:这是否也适用于像2014年12月03日、2014年12月01日、2014年11月30日这样的数字,这样2014年12月02日之后的所有数字都会显示出来?可能吧,但最好将这些字符串转换为DateTime对象并进行比较。这样更可靠,很接近。如果数组中有2014_12_02或2014_11_30这样的数字会怎么样?2014年12月1日之后是否有将所有项目分开的选项?