note 92019 deleted from function.settype by danbrown
| From: | danbrown@php.net | Date: | Sun, 05 Jul 2009 17:49:02 +0000 |
| Subject: | note 92019 deleted from function.settype by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157943@lists.php.net to get a copy of this message | ||
Note Submitter: david[ dot ]scourfield[ at nospam!]llynfi dot co dot uk
----
in response to NWdev, the problem with your original code was that the argument to your custom
function is being passed _by value_ so the original variable remains unchanged. Simply changing
your code to this will solve the problem:
<?php
//$numArray is generated by another process
$numArray = array('13','14','33');
var_dump($numArray);
// conversion function
function str_to_int(&$val){
// note the ampersand (&) character before the parameter above
// -> this makes sure the argument is passed _by reference_
// -> instead of by value
settype($val,'int');
}
array_walk($numArray,'str_to_int');
var_dump($numArray);
/*
outputs:
array(3) {
[0]=>
string(2) "13"
[1]=>
string(2) "14"
[2]=>
string(2) "33"
}
array(3) {
[0]=>
int(13)
[1]=>
int(14)
[2]=>
int(33)
}
*/
?>
Regards, David