I have the following array
$arr = array(
"person1" => 10,
"person2" => 10,
"person3" => 20,
"person4" => 25,
"person5" => 35,
);
I would like to write a function that takes $arr
as an argument and returns 3 elements of the array based on the values stored in each element.
For example if the returned subset yields
$newArr = array(
"person5" => 35,
"person1" => 10,
"person4" => 25,
);
There was a 35% chance that person5 would be the first element stored in $newArr
based on the value stored in $arr['person5']
divided by the sum of values stored in the remaining elements.
$arr['person5']/($arr['person5'] + $arr['person4'] + $arr['person3'] + $arr['person2'] + $arr['person1'])
There was a ~15% chance that person1 would be the second element stored in $newArr
based on the value stored in $arr['person1']
divided by the sum of values stored in the remaining elements.
$arr['person1']/($arr['person4'] + $arr['person3'] + $arr['person2'] + $arr['person1'])
There was a ~45% chance that person4 would be the second element stored in $newArr
based on the value stored in $arr['person4']
divided by the sum of values stored in the remaining elements.
$arr['person4']/($arr['person4'] + $arr['person3'] + $arr['person2'])
How could I write a function that does this?