Reputation: 13
I´m looking for a solution to get all words (or numbers) from a sorted array AFTER some letter or number. I.e. all countries after letter 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);
That will return Belgium, Finland, France, Georgia, Germany, Greece, Hungary, Iceland, Ireland, Italy, Lithuania, Luxembourg, Malta, Netherlands, Norway, ...
But I want only countries AFTER letter K: Lithuania, Luxembourg, Malta, Netherlands, Norway, ...
Any ideas?
Upvotes: 1
Views: 194
Reputation: 13
I finally find a simple solution to splice an array after any delimiter. Even if it´s not a letter or digit (like "2013_12_03"). Just insert the wanted delimiter into the array, then order, then splice:
//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
Hope that helps all who wants to splice a naturally ordered array after something.
Upvotes: 0
Reputation: 3636
Use the array_filter
function to filter out the stuff you don't want.
$result = array_filter( $countries, function( $country ) {
return strtoupper($country{0}) > "K";
});
Upvotes: 3
Reputation: 593
You can do this:
$countries2 = array();
foreach ($countries as $country) {
if(strtoupper($country[0]) > "K") break;
$countries2[] = $country;
}
Upvotes: 0