将值数组转换为PHP中的单个浮点值? [英] Convert array of values into a single float value in PHP?

查看:55
本文介绍了将值数组转换为PHP中的单个浮点值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有这些值的数组(当使用 print_r()打印该数组时

 数组:[0] =>66[1] =>233[2] =>204[3] =>205 

十六进制的值是:

 数组:[0] =>0x42[1] =>0xE9[2] =>0xCC[3] =>0xCD 

我想要做的是将这4个字节的数组转换为浮点值.如果我使用 implode(); 将数组转换为值,则它将字符串组合成 66233204205 而不是类似的 0x42E9CCCD .因此,我不能使用 floatval().PHP对我来说是新手,所以也像我在C语言中那样使用字符串值而不是实际位.

我在想的是如何使用十六进制值而不是那些整数将 implode()插入,然后使用 floatval().

>

有什么想法的人吗?

这样就更清楚了,我应该得到 116.900 作为结果

解决方案

已修订的答案为....

使用十六进制字符串执行数学运算曾经是PHP支持的功能.现在,在PHP 7中,十六进制字符串仅代表字符串,不再被识别为包含数字值.如果尝试对其进行数学运算,则结果为零.考虑以下代码:

 <?php$ arr = [66,233,204,205];$ res = array_reduce($ arr,function($ c,$ i){$ c.= dechex($ i);返回$ c;});$ temp ="0x".$ res;//0x42e9cccdvar_dump($ temp + 0); 

请参见演示

此代码尝试通过向$ temp中包含的值加上零来为十六进制字符串提供数学上下文.该代码在PHP 7之前一直有效,因为确定 hexstrings 产生的功能多于其应有的价值.请参见 RFC 和手册:十六进制字符串不再被视为数字" .

串联是一个字符串运算,它创建示例的十六进制字符串,其直接用法在数学运算中被证明是不明智的.将发出一条通知(在PHP 7.1中),抱怨如下:

注意:遇到格式不正确的数值

您可以禁止显示此通知,但是在PHP 7中结果总和为零.当代码在PHP 5.6中正确运行时, 1122618573 的结果似乎是错误的,肯定太大而无法转换作为浮点数,并获取OP寻求的值.

...善意的工作环境

 <?php$ arr = [66,233,204,205];$ res = array_reduce($ arr,function($ c,$ i){$ c.= dechex($ i);返回$ c;});$ temp ="0x".$ res;$ int = filter_var($ temp,FILTER_VALIDATE_INT,FILTER_FLAG_ALLOW_HEX);如果(false === $ int){抛出新的Exception("Invalid integer!");}$ arr =(unpack('f',pack('i',$ int))));$ f = array_pop($ arr);printf(%.3f",$ f); 

请参见演示

如果使用带指定参数的filter_var(),PHP将识别array_reduce()产生的十六进制数字字符串.以这种方式,您可以获得一个整数,其值为 1122618573 .关键而不是整数的 value 是其二进制位模式.从官方答案此处借用,代码需要 解决方案

Revised Answer with ....

Performing math with hex strings used to be a feature supported in PHP. Now with PHP 7, a hex string only represents a string of characters and no longer is recognized as containing a numeric value. If you attempt to do math with it, the result is zero. Consider the following code:

 <?php  

 $arr = [66, 233, 204, 205];

 $res = array_reduce( $arr, function($c,$i) {
                   $c.=dechex( $i );
                   return $c;
 });

 $temp = "0x" . $res;  // 0x42e9cccd
 var_dump($temp + 0);

See demo

This code attempts to provide the hex string a mathematical context by adding zero to the value contained in $temp. This code works until PHP 7 because the powers that be determined that hexstrings created more problems than they were worth; see this RFC and the Manual:"Hexadecimal strings are no longer considered numeric".

Concatenation, being a string operation, creates the example's hex string whose direct usage proves unwise in a math operation. A notice will be emitted (in PHP 7.1), complaining as follows:

Notice: A non well formed numeric value encountered

You may suppress displaying this notice, but the resulting sum will be zero in PHP 7. When the code functions correctly in PHP 5.6, the result of 1122618573 seems wrong, certainly far too large to cast as a float and obtain the value that the OP seeks.

... A Bona Fide Work-Around

    <?php


    $arr = [66, 233, 204, 205];
    $res = array_reduce( $arr, function($c,$i) {
                       $c.=dechex( $i );
                       return $c;
    });
    $temp = "0x" . $res;
    $int = filter_var( $temp, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX );
    if (false === $int) {
        throw new Exception("Invalid integer!");
    }


    $arr = (unpack('f', pack('i', $int )));
    $f = array_pop($arr);
    printf("%.3f",$f);    

See demo

PHP will recognize the numeric hex string that array_reduce() yields if you use filter_var() with the indicated parameters. In this fashion, you may obtain an integer evaluating as 1122618573. The key thing rather than the integer's value is its binary bit pattern. Borrowing from the official answer here, the code needs to pack $int into a binary string, which it subsequently will unpack as a float -- almost. That result will be returned as an array with just one element. After popping off and capturing that element's float value, printf() displays 116.900.

这篇关于将值数组转换为PHP中的单个浮点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆