-
-
Save skrajewski/7836843da573df5eb19439e77dad23fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Country | |
{ | |
private $name; | |
private $code; | |
public function __construct($name, $code) | |
{ | |
$this->name = $name; | |
$this->code = $code; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function getCode() | |
{ | |
return $this->code; | |
} | |
} | |
$data = [ | |
new Country('Afghanistan', 'AF'), | |
new Country('Åland Islands', 'AX'), | |
new Country('Albania', 'AL'), | |
new Country('Algeria', 'DZ'), | |
new Country('American Samoa', 'AS'), | |
new Country('Andorra', 'AD'), | |
new Country('Angola', 'AO'), | |
new Country('Anguilla', 'AI'), | |
new Country('Antarctica', 'AQ'), | |
new Country('Antigua and Barbuda', 'AG'), | |
new Country('Argentina', 'AR'), | |
new Country('Armenia', 'AM'), | |
new Country('Aruba', 'AW'), | |
new Country('Australia', 'AU'), | |
new Country('Austria', 'AT'), | |
new Country('Azerbaijan', 'AZ'), | |
new Country('Bahamas', 'BS'), | |
new Country('Bahrain', 'BH'), | |
new Country('Bangladesh', 'BD'), | |
new Country('Barbados', 'BB'), | |
new Country('Belarus', 'BY'), | |
new Country('Belgium', 'BE'), | |
new Country('Belize', 'BZ'), | |
new Country('Benin', 'BJ'), | |
new Country('Bermuda', 'BM'), | |
new Country('Bhutan', 'BT'), | |
new Country('Bolivia', 'BO'), | |
new Country('Bonaire, Sint Eustatius and Saba', 'BQ'), | |
new Country('Bosnia and Herzegovina', 'BA'), | |
new Country('Botswana', 'BW'), | |
new Country('Bouvet Island', 'BV'), | |
new Country('Brazil', 'BR'), | |
new Country('British Indian Ocean Territory', 'IO'), | |
new Country('British Virgin Islands', 'VG'), | |
new Country('Brunei Darussalam', 'BN'), | |
new Country('Bulgaria', 'BG'), | |
new Country('Burkina Faso', 'BF'), | |
new Country('Burundi', 'BI'), | |
new Country('Cambodia', 'KH'), | |
new Country('Cameroon', 'CM'), | |
new Country('Canada', 'CA'), | |
new Country('Cape Verde', 'CV'), | |
new Country('Cayman Islands', 'KY'), | |
new Country('Central African Republic', 'CF'), | |
new Country('Chad', 'TD'), | |
new Country('Chile', 'CL'), | |
new Country('China', 'CN'), | |
new Country('Christmas Island', 'CX'), | |
new Country('Cocos (Keeling) Islands', 'CC'), | |
new Country('Colombia', 'CO'), | |
new Country('Comoros', 'KM'), | |
new Country('Congo', 'CG'), | |
new Country('Cook Islands', 'CK'), | |
new Country('Costa Rica', 'CR'), | |
new Country('Côte d\'Ivoire', 'CI'), | |
new Country('Croatia', 'HR'), | |
new Country('Cuba', 'CU'), | |
new Country('Curaçao', 'CW'), | |
new Country('Cyprus', 'CY'), | |
new Country('Czech Republic', 'CZ'), | |
new Country('Democratic Republic of the Congo', 'CD'), | |
new Country('Denmark', 'DK'), | |
new Country('Djibouti', 'DJ'), | |
new Country('Dominica', 'DM'), | |
new Country('Dominican Republic', 'DO'), | |
new Country('East Timor', 'TL'), | |
new Country('Ecuador', 'EC'), | |
new Country('Egypt', 'EG'), | |
new Country('El Salvador', 'SV'), | |
new Country('Equatorial Guinea', 'GQ'), | |
new Country('Eritrea', 'ER'), | |
new Country('Estonia', 'EE'), | |
new Country('Ethiopia', 'ET'), | |
new Country('Falkland Islands', 'FK'), | |
new Country('Faroe Islands', 'FO'), | |
new Country('Federated States of Micronesia', 'FM'), | |
new Country('Fiji', 'FJ'), | |
new Country('Finland', 'FI'), | |
new Country('France', 'FR'), | |
new Country('French Guiana', 'GF'), | |
new Country('French Polynesia', 'PF'), | |
new Country('French Southern and Antarctic Lands', 'TF'), | |
new Country('Gabon', 'GA'), | |
new Country('Gambia', 'GM'), | |
new Country('Georgia', 'GE'), | |
new Country('Germany', 'DE'), | |
new Country('Ghana', 'GH'), | |
new Country('Gibraltar', 'GI'), | |
new Country('Greece', 'GR'), | |
new Country('Greenland', 'GL'), | |
new Country('Grenada', 'GD'), | |
new Country('Guadeloupe', 'GP'), | |
new Country('Guam', 'GU'), | |
new Country('Guatemala', 'GT'), | |
new Country('Guernsey', 'GG'), | |
new Country('Guinea', 'GN'), | |
new Country('Guinea-Bissau', 'GW'), | |
new Country('Guyana', 'GY'), | |
new Country('Haiti', 'HT'), | |
new Country('Heard Island and McDonald Islands', 'HM'), | |
new Country('Honduras', 'HN'), | |
new Country('Hong Kong', 'HK'), | |
new Country('Hungary', 'HU'), | |
new Country('Iceland', 'IS'), | |
new Country('India', 'IN'), | |
new Country('Indonesia', 'ID'), | |
new Country('Iran', 'IR'), | |
new Country('Iraq', 'IQ'), | |
new Country('Ireland', 'IE'), | |
new Country('Isle of Man', 'IM'), | |
new Country('Israel', 'IL'), | |
new Country('Italy', 'IT'), | |
new Country('Jamaica', 'JM'), | |
new Country('Japan', 'JP'), | |
new Country('Jersey', 'JE'), | |
new Country('Jordan', 'JO'), | |
new Country('Kazakhstan', 'KZ'), | |
new Country('Kenya', 'KE'), | |
new Country('Kiribati', 'KI'), | |
new Country('Kuwait', 'KW'), | |
new Country('Kyrgyzstan', 'KG'), | |
new Country('Laos', 'LA'), | |
new Country('Latvia', 'LV'), | |
new Country('Lebanon', 'LB'), | |
new Country('Lesotho', 'LS'), | |
new Country('Liberia', 'LR'), | |
new Country('Libya', 'LY'), | |
new Country('Liechtenstein', 'LI'), | |
new Country('Lithuania', 'LT'), | |
new Country('Luxembourg', 'LU'), | |
new Country('Macau', 'MO'), | |
new Country('Macedonia', 'MK'), | |
new Country('Madagascar', 'MG'), | |
new Country('Malawi', 'MW'), | |
new Country('Malaysia', 'MY'), | |
new Country('Maldives', 'MV'), | |
new Country('Mali', 'ML'), | |
new Country('Malta', 'MT'), | |
new Country('Marshall Islands', 'MH'), | |
new Country('Martinique', 'MQ'), | |
new Country('Mauritania', 'MR'), | |
new Country('Mauritius', 'MU'), | |
new Country('Mayotte', 'YT'), | |
new Country('Mexico', 'MX'), | |
new Country('Moldova', 'MD'), | |
new Country('Monaco', 'MC'), | |
new Country('Mongolia', 'MN'), | |
new Country('Montenegro', 'ME'), | |
new Country('Montserrat', 'MS'), | |
new Country('Morocco', 'MA'), | |
new Country('Mozambique', 'MZ'), | |
new Country('Myanmar', 'MM'), | |
new Country('Namibia', 'NA'), | |
new Country('Nauru', 'NR'), | |
new Country('Nepal', 'NP'), | |
new Country('Netherlands', 'NL'), | |
new Country('New Caledonia', 'NC'), | |
new Country('New Zealand', 'NZ'), | |
new Country('Nicaragua', 'NI'), | |
new Country('Niger', 'NE'), | |
new Country('Nigeria', 'NG'), | |
new Country('Niue', 'NU'), | |
new Country('Norfolk Island', 'NF'), | |
new Country('Northern Mariana Islands', 'MP'), | |
new Country('North Korea', 'KP'), | |
new Country('Norway', 'NO'), | |
new Country('Oman', 'OM'), | |
new Country('Pakistan', 'PK'), | |
new Country('Palau', 'PW'), | |
new Country('Palestine', 'PS'), | |
new Country('Panama', 'PA'), | |
new Country('Papua New Guinea', 'PG'), | |
new Country('Paraguay', 'PY'), | |
new Country('Peru', 'PE'), | |
new Country('Philippines', 'PH'), | |
new Country('Pitcairn', 'PN'), | |
new Country('Poland', 'PL'), | |
new Country('Portugal', 'PT'), | |
new Country('Puerto Rico', 'PR'), | |
new Country('Qatar', 'QA'), | |
new Country('Réunion', 'RE'), | |
new Country('Romania', 'RO'), | |
new Country('Russia', 'RU'), | |
new Country('Rwanda', 'RW'), | |
new Country('Saint Barthélemy', 'BL'), | |
new Country('Saint Helena, Ascension and Tristan da Cunha', 'SH'), | |
new Country('Saint Kitts and Nevis', 'KN'), | |
new Country('Saint Lucia', 'LC'), | |
new Country('Saint Martin', 'MF'), | |
new Country('Saint Pierre and Miquelon', 'PM'), | |
new Country('Saint Vincent and the Grenadines', 'VC'), | |
new Country('Samoa', 'WS'), | |
new Country('San Marino', 'SM'), | |
new Country('São Tomé and Príncipe', 'ST'), | |
new Country('Saudi Arabia', 'SA'), | |
new Country('Senegal', 'SN'), | |
new Country('Serbia', 'RS'), | |
new Country('Seychelles', 'SC'), | |
new Country('Sierra Leone', 'SL'), | |
new Country('Singapore', 'SG'), | |
new Country('Sint Maarten', 'SX'), | |
new Country('Slovakia', 'SK'), | |
new Country('Slovenia', 'SI'), | |
new Country('Solomon Islands', 'SB'), | |
new Country('Somalia', 'SO'), | |
new Country('South Africa', 'ZA'), | |
new Country('South Georgia and the South Sandwich Islands', 'GS'), | |
new Country('South Korea', 'KR'), | |
new Country('South Sudan', 'SS'), | |
new Country('Spain', 'ES'), | |
new Country('Sri Lanka', 'LK'), | |
new Country('Sudan', 'SD'), | |
new Country('Suriname', 'SR'), | |
new Country('Svalbard and Jan Mayen', 'SJ'), | |
new Country('Swaziland', 'SZ'), | |
new Country('Sweden', 'SE'), | |
new Country('Switzerland', 'CH'), | |
new Country('Syria', 'SY'), | |
new Country('Taiwan', 'TW'), | |
new Country('Tajikistan', 'TJ'), | |
new Country('Tanzania', 'TZ'), | |
new Country('Thailand', 'TH'), | |
new Country('Togo', 'TG'), | |
new Country('Tokelau', 'TK'), | |
new Country('Tonga', 'TO'), | |
new Country('Trinidad and Tobago', 'TT'), | |
new Country('Tunisia', 'TN'), | |
new Country('Turkey', 'TR'), | |
new Country('Turkmenistan', 'TM'), | |
new Country('Turks and Caicos Islands', 'TC'), | |
new Country('Tuvalu', 'TV'), | |
new Country('Uganda', 'UG'), | |
new Country('Ukraine', 'UA'), | |
new Country('United Arab Emirates', 'AE'), | |
new Country('United Kingdom', 'GB'), | |
new Country('United States', 'US'), | |
new Country('United States Minor Outlying Islands', 'UM'), | |
new Country('United States Virgin Islands', 'VI'), | |
new Country('Uruguay', 'UY'), | |
new Country('Uzbekistan', 'UZ'), | |
new Country('Vanuatu', 'VU'), | |
new Country('Vatican City', 'VA'), | |
new Country('Venezuela', 'VE'), | |
new Country('Vietnam', 'VN'), | |
new Country('Wallis and Futuna', 'WF'), | |
new Country('Western Sahara', 'EH'), | |
new Country('Yemen', 'YE'), | |
new Country('Zambia', 'ZM'), | |
new Country('Zimbabwe', 'ZW') | |
]; | |
function mapForCombo_foreach(array $array, Closure $fnValue, Closure $fnKey): array | |
{ | |
$output = []; | |
foreach ($array as $element) { | |
$output[$fnKey($element)] = $fnValue($element); | |
} | |
return $output; | |
} | |
function mapForCombo_map(array $array, Closure $fnValue, Closure $fnKey): array | |
{ | |
$keys = array_map($fnKey, $array); | |
$values = array_map($fnValue, $array); | |
return array_combine($keys, $values); | |
} | |
function mapForCombo_reduce(array $array, Closure $fnValue, Closure $fnKey): array | |
{ | |
return array_reduce($array, function ($acc, $element) use ($fnKey, $fnValue) { | |
$acc[$fnKey($element)] = $fnValue($element); | |
return $acc; | |
}, []); | |
} | |
function test($function, $tests, $data) { | |
$start = microtime(true); | |
while ($tests--) { | |
$function( | |
$data, | |
function (Country $country) { return $country->getCode(); }, | |
function (Country $country) { return $country->getName(); } | |
); | |
} | |
$duration = microtime(true) - $start; | |
return $duration; | |
} | |
for ($i = 0; $i < 10; $i++) { | |
$res1 = test('mapForCombo_foreach', 5000, $data); | |
$res2 = test('mapForCombo_map', 5000, $data); | |
$res3 = test('mapForCombo_reduce', 5000, $data); | |
echo "$res1,$res2,$res3".PHP_EOL; | |
} | |
Thank you so much, Szymon.
This was really helpful to me. I was trying to process an array already populated from a database query in another PHP file, instead of querying the DB again for this file.
And my PHP version was not supporting the array_column() function, your custom function was just what I needed.
I'm glad I could help :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much, Szymon.
This was really helpful to me. I was trying to process an array already populated from a database query in another PHP file, instead of querying the DB again for this file.
And my PHP version was not supporting the array_column() function, your custom function was just what I needed.