Last active
February 27, 2018 23:09
-
-
Save dingo-d/aa13dcac02872c294ebe416f8e17caab to your computer and use it in GitHub Desktop.
A cool example of a php closure. Say you have a multidimensional array of values, and you want to get the certain value out of it? You can use array_values, array_filter and array_map to get the value from the array using closures in php.
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 | |
$currency_helper = array( | |
'1' => 'HRK', | |
'2' => 'AUD', | |
'3' => 'CAD', | |
'4' => 'CZK', | |
'5' => 'DKK', | |
'6' => 'HUF', | |
'7' => 'JPY', | |
'8' => 'NOK', | |
'9' => 'SEK', | |
'10' => 'CHF', | |
'11' => 'GBP', | |
'12' => 'USD', | |
'13' => 'BAM', | |
'14' => 'EUR', | |
'15' => 'PLN', | |
); | |
// List taken from http://hnbex.eu/api/v1/rates/daily/ on 27.2.2018. | |
$json = '[{"unit_value": 1, "buying_rate": "4.730403", "selling_rate": "4.758871", "currency_code": "AUD", "median_rate": "4.744637"}, {"unit_value": 1, "buying_rate": "4.756792", "selling_rate": "4.785418", "currency_code": "CAD", "median_rate": "4.771105"}, {"unit_value": 1, "buying_rate": "0.292159", "selling_rate": "0.293917", "currency_code": "CZK", "median_rate": "0.293038"}, {"unit_value": 1, "buying_rate": "0.996336", "selling_rate": "1.002332", "currency_code": "DKK", "median_rate": "0.999334"}, {"unit_value": 100, "buying_rate": "2.368373", "selling_rate": "2.382625", "currency_code": "HUF", "median_rate": "2.375499"}, {"unit_value": 100, "buying_rate": "5.636943", "selling_rate": "5.670867", "currency_code": "JPY", "median_rate": "5.653905"}, {"unit_value": 1, "buying_rate": "0.770900", "selling_rate": "0.775540", "currency_code": "NOK", "median_rate": "0.773220"}, {"unit_value": 1, "buying_rate": "0.739139", "selling_rate": "0.743587", "currency_code": "SEK", "median_rate": "0.741363"}, {"unit_value": 1, "buying_rate": "6.437749", "selling_rate": "6.476491", "currency_code": "CHF", "median_rate": "6.457120"}, {"unit_value": 1, "buying_rate": "8.450920", "selling_rate": "8.501778", "currency_code": "GBP", "median_rate": "8.476349"}, {"unit_value": 1, "buying_rate": "6.008113", "selling_rate": "6.044271", "currency_code": "USD", "median_rate": "6.026192"}, {"unit_value": 1, "buying_rate": "3.792874", "selling_rate": "3.815700", "currency_code": "BAM", "median_rate": "3.804287"}, {"unit_value": 1, "buying_rate": "7.418217", "selling_rate": "7.462861", "currency_code": "EUR", "median_rate": "7.440539"}, {"unit_value": 1, "buying_rate": "1.778608", "selling_rate": "1.789312", "currency_code": "PLN", "median_rate": "1.783960"}]'; | |
$api_rates = json_decode( $json, true ); // Get an array of values. | |
$currency = $currency_helper[ 14 ]; // Get certain currency. In our case it's EUR. | |
$currency_rate = array_values( array_filter( array_map( function( $el ) use ( $currency ) { // Notice the use keyword. | |
if ( $el['currency_code'] === $currency ) { | |
return $el['median_rate']; | |
} | |
}, $api_rates ) ) ); | |
print_r( $currency_rate[0] ); // We got what we wanted. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment