简体   繁体   中英

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

I am trying to make a app that allow me to rank and sort decimal number according to it's value , but array_flip function can't flip string and decimal number ,

 <?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);


?>

and there is a warning about array_flip

Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in C:\\xampp\\htdocs\\ranking.php on line 9

, do you guys know how to deal with these ? is there any solution ?

After sort use array_walk to convert each item from decimal to string like ---

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

And then flip it. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM