简体   繁体   English

如何从数组中获取一个数组值?

[英]How to fetch one array value from the array?

Hi I have array like this嗨,我有这样的数组

Array ( 
  [0] => stdClass Object ( 
     [id] => 1930 [value] => 20)

  [1] => stdClass Object (

     [id] => 1931 [value] => 30 ) 

  [2] => stdClass Object ( 

     [id] => 1937 [value] => 30 ) 

  [3] => stdClass Object ( 

     [id] => 1938 [value] => 20 ) 
)

I want to fetch random array from this.我想从中获取随机数组。 The Id which has greater value(%) should be fetched more time (That value is %).具有更大值(%)的 Id 应该被获取更多时间(该值是 %)。

Make a another array and repeat the index of array for the number of times its value is and then fetch the values randomly this would do what you want制作另一个数组并重复数组的索引为其值的次数,然后随机获取值这会做你想要的

The above mentioned array would be上面提到的数组将是

array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,)

$arr = Array ( 
  (Object) array (id => 1930, value => 20),
  (Object) array (id => 1931, value => 30),
  (Object) array (id => 1937, value => 30),
  (Object) array (id => 1938, value => 20));

// count sum of all values
$sum = array_reduce($arr, function ($c, $i) { return $c += $i->value; }, 0);
// get random 
$rand = mt_rand(0, $sum);
echo $rand . "  ";
// select 1st item that sum of values >= random value
$new = array_filter($arr, function ($i) use (&$rand) { if($rand > 0 && ($rand -= $i->value) <= 0) return true; else return false; });
// change key to 0
$new = array_values($new);
echo  $new[0]->id;

One way to solve this is to make a list of the cummulative sum of the values in the array, for instance if the values are 3, 1, and 2 the cummulative sums would be 3, 4 and 6. Then when you want to get a random element, you generate a random integer between 1 maximum sum, and then pick the element whose cumulative sum is largest of those whos cumulative sum is smaller than or equal to the random number.解决这个问题的一种方法是列出数组中值的累积总和,例如,如果值是 3、1 和 2,则累积总和将为 3、4 和 6。然后当你想要得到一个随机元素,您生成一个介于 1 最大和之间的随机整数,然后从累积和小于或等于随机数的元素中选择累积和最大的元素。 In that way, the chanse of any element being picked is proportional to its value.这样,被选取的任何元素的 chanse 与其值成正比。

So here is the setup, creating the cumulative sums:所以这是设置,创建累积总和:

var $cumsum = array();
var $sum = 0;
foreach($array as $obj) {
    $sum += $obj->value;
    array_push($cumsum, $sum);
}
var $maxsum = end($cumsum);

And here is the function actually picking an element:这是实际选择元素的函数:

function getRandomObject() {
    var $r = random(1, $max);
    if($r < $cumsum[0]) return $array[0];
    for($i = 0; $i < count($cumsum); $i++)
        if($cumsum[$i]) > $r)
            return $array[$i--];
    return $array[$i];
}

Disclaimar: I have not tested this code, so don't expect it to run on a copy paste.免责声明:我没有测试过这段代码,所以不要指望它在复制粘贴上运行。 Also, whatever code you use you should probably make sure it returns elements with the right probability using a monte carlo method.此外,无论您使用什么代码,您都应该确保它使用蒙特卡罗方法以正确的概率返回元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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