如何在数组PHP中翻转STRING和十进制/浮点值


how to flip flip STRING and decimal/ float values in array PHP?

我正在尝试制作一个应用程序,允许我根据十进制数的值对其进行排名和排序,但array_flicp函数无法翻转字符串和十进制数,

 <?php
$myarray = array(1,0.334,-0.334,-1);
//create a copy and sort
$myarray_copy = $myarray;
rsort($myarray_copy);
//reverses key and values
$myarray_copy = array_flip($myarray_copy);
//create result by using keys from sorted values + 1
foreach($myarray as $val)
    $myarray2[] = ($myarray_copy[$val]+1);
//print final array
print_r($myarray2);
print_r($myarray);

?>

并且存在关于array_flicp 的警告

警告:array_flip()[function.array flip]:只能翻转STRING和INTEGER值!在C:''examplep''htdocs''ranking.php中的第9行

,你们知道怎么处理这些吗?有什么解决办法吗?

排序后,使用array_walk将每个项目从十进制转换为字符串,如---

function test_alter(&$item1, $key)
{
    $item1 = (string)$item1;
}
array_walk($fruits, 'test_alter', 'fruit');

然后翻转它。希望这能有所帮助。