Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I just want to use array_walk() with ceil() to round all the elements within an array. But it doesn't work.

The code:

$numbs = array(3, 5.5, -10.5); 
array_walk($numbs, "ceil"); 
print_r($numbs);  

output should be: 3,6,-10

The error message:

Warning: ceil() expects exactly 1 parameter, 2 given on line 2

output is: 3,5.5,-10.5 (Same as before using ceil())

I also tried with round().

share|improve this question

4 Answers 4

Use array_map instead.

$numbs = array(3, 5.5, -10.5);
$numbs = array_map("ceil", $numbs);
print_r($numbs);

array_walk actually passes 2 parameters to the callback, and some built-in functions don't like being called with too many parameters (there's a note about this on the docs page for array_walk). This is just a Warning though, it's not an error.

array_walk also requires that the first parameter of the callback be a reference if you want it to modify the array. So, ceil() was still being called for each element, but since it didn't take the value as a reference, it didn't update the array.

array_map is better for this situation.

share|improve this answer
1  
That works fine. Thanks –  Quarter 5 hours ago
    
You're welcome :-) Sometimes one array_* function is better than another, depending on the situation. –  Rocket Hazmat 5 hours ago

I had the same problem with another PHP function. You can create "your own ceil function". In that case it is very easy to solve:

function myCeil(&$list){  
    $list =  ceil($list);  
}  

$numbs = array(3, 5.5, -10.5);  
array_walk($numbs, "myCeil");  
print_r($numbs);  
share|improve this answer
1  
That works fine. Awsome code. Thanks –  Quarter 5 hours ago

That is because array_walk needs function which first parameter is a reference

function myCeil(&$value){
    $value = ceil($value);
}

$numbs = array(3, 5.5, -10.5); 
array_walk($numbs, "myCeil"); 
print_r($numbs); 
share|improve this answer

The reason it doesn't work is because ceil($param) expects only one parameter instead of two.

What you can do:

$numbs = array(3, 5.5, -10.5); 
array_walk($numbs, function($item) {
    echo ceil($item);
}); 

If you want to save these values then go ahead and use array_map which returns an array.

Hope this helps,

share|improve this answer
    
Not quite. The warning says "ceil() expects exactly 1 parameter, 2 given". array_walk will call your callback function and pass it 2 parameters ($value and $key). The array wasn't getting updated because array_walk will only do that if the $value parameter is a reference (and its value is updated in the callback). –  Rocket Hazmat 5 hours ago
2  
Rephrased thanks! :) –  flangofas 5 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.