Last active
April 14, 2021 07:24
-
-
Save minte9/6cb1445b0cb49e56620797b2a2d5a9d0 to your computer and use it in GitHub Desktop.
Php / Basics / Reference
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 | |
/** | |
* array_map() | |
* | |
* Round up array values | |
* Use array_map() with built-in ceil functions | |
* The result array $mapped will be [2, 3, 4] | |
*/ | |
/** | |
* SOLUTION | |
*/ | |
$arr = [1.5, 2.6, 3.7]; | |
$mapped = array_map('ceil', $arr); | |
print_r($mapped); // [2, 3, 4] | |
/** | |
* array_walk() | |
* | |
* Round up array values | |
* Use array_walk() with reference | |
* The array will be [2, 3, 4] | |
*/ | |
/** | |
* SOLUTION | |
*/ | |
$arr = [1.5, 2.6, 3.7]; | |
array_walk($arr, function(&$v) { | |
$v = ceil($v); | |
}); | |
print_r($arr); // [2, 3, 4] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment