Skip to content

Instantly share code, notes, and snippets.

@minte9
Last active April 14, 2021 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minte9/6cb1445b0cb49e56620797b2a2d5a9d0 to your computer and use it in GitHub Desktop.
Save minte9/6cb1445b0cb49e56620797b2a2d5a9d0 to your computer and use it in GitHub Desktop.
Php / Basics / Reference
<?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