开发者

php, how to treat arrays with floats inside?

example:

the following is correct?

$var = floatval($开发者_JAVA百科arr[2]) ;

"You cannot use floatval() on arrays..." maybe that's an old directive or how to edit...?


That quote from the manual (which BTW doesn't seem to exist in the current manual anymore) only means that you can't use floatval on values that are arrays, i.e.:

$foo = array();
$bar = floatval($foo);

Which, BTW, is not entirely correct, since it would produce either 1.0 or 0.0, depending on whether the array was empty or not.* It just doesn't make much sense. If you access a scalar value inside an array, that's not using "floatval on an array". I.e., this works perfectly fine:

$foo = array("42.1231");
$bar = floatval($foo[0]);

That's using the scalar value in $foo[0], whether that's in an array or not is irrelevant.


* The manual now clearly says Empty arrays return 0, non-empty arrays return 1. Maybe this behavior has changed?


you will need to traverse the array and perform the operation on each element:

foreach($arr as $id=>$elem){
  $arr[$id]=floatval($elem);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜