PHP随机洗牌数组维护重点= GT;值 [英] PHP Random Shuffle Array Maintaining Key => Value

查看:120
本文介绍了PHP随机洗牌数组维护重点= GT;值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找对谷歌的答案,但似乎无法找到的东西防呆和真的不能负担搞砸了(去住到生产站点)。

我已经是20+的过滤器,它返回一个数组,包括一个ID和一个距离的高级搜索。我需要做的就是洗牌这些结果随机顺序,每次显示。这该阵列我有,在目前出来的是:

 阵列(
    [0] =>阵列([ID] => 1 [距离] => 1.95124994507577)
    [1] =>阵列([ID] => 13 [距离] => 4.75358968511882)
    [2] =>阵列([ID] =大于7 [距离] => 33.2223233233323)
    [3] =>阵列([ID] => 21 [距离] => 18.2155453552336)
    [4] =>阵列([ID] => 102 [距离] = 221.2212587899658)

我需要什么,能够做的就是随机化或这些每一次订单,但保持ID和距离对,即:

 阵列(
    [4] =>阵列([ID] => 102 [距离] = 221.2212587899658)
    [1] =>阵列([ID] => 13 [距离] => 4.75358968511882)
    [3] =>阵列([ID] => 21 [距离] => 18.2155453552336)
    [2] =>阵列([ID] =大于7 [距离] => 33.2223233233323)
    [0] =>阵列([ID] => 1 [距离] => 1.95124994507577)

感谢:)


解决方案

网友发帖 随机 文档下:


  

随机播放和联想
  非关联数组,而preserving
  键,值对。也返回
  洗牌数组,而不是它的洗牌
  到位。


 函数shuffle_assoc($名单){
  如果返回$列表(is_array($名单)!);  $键= array_keys($清单);
  洗牌($键);
  $随机=阵列();
  的foreach($键为$键){
    $随机[$关键] = $名单[$关键];
  }
  返回$随机的;
}

测试用例:

  $ ARR =阵列();
$常用3 [] =阵列('ID'=> 5,'富'=>'你好');
$常用3 [] =阵列('ID'=大于7,'富'=>'BYEBYE');
$常用3 [] =阵列('ID'=> 9,'富'=>'富');
的print_r(shuffle_assoc($ ARR));
的print_r(shuffle_assoc($ ARR));
的print_r(shuffle_assoc($ ARR));

I've been looking on google for the answer but can't seem to find something fool-proof and cant really afford to mess this up (going live into a production site).

What I have is an advanced search with 20+ filters, which returns an array including an ID and a Distance. What I need to do is shuffle these results to display in a random order every time. The array I have that comes out at the moment is:

Array (
    [0] => Array ( [id] => 1 [distance] => 1.95124994507577 )
    [1] => Array ( [id] => 13 [distance] => 4.75358968511882 )
    [2] => Array ( [id] => 7 [distance] => 33.2223233233323 )
    [3] => Array ( [id] => 21 [distance] => 18.2155453552336 )
    [4] => Array ( [id] => 102 [distance] = 221.2212587899658 )
)

What I need to be able to do is randomise or order of these every time but maintain the id and distance pairs, i.e.:

Array (
    [4] => Array ( [id] => 102 [distance] = 221.2212587899658 )
    [1] => Array ( [id] => 13 [distance] => 4.75358968511882 )
    [3] => Array ( [id] => 21 [distance] => 18.2155453552336 )
    [2] => Array ( [id] => 7 [distance] => 33.2223233233323 )
    [0] => Array ( [id] => 1 [distance] => 1.95124994507577 )
)

Thanks :)

解决方案

The first user post under the shuffle documentation:

Shuffle associative and non-associative array while preserving key, value pairs. Also returns the shuffled array instead of shuffling it in place.

function shuffle_assoc($list) { 
  if (!is_array($list)) return $list; 

  $keys = array_keys($list); 
  shuffle($keys); 
  $random = array(); 
  foreach ($keys as $key) { 
    $random[$key] = $list[$key]; 
  }
  return $random; 
} 

Test case:

$arr = array();
$arr[] = array('id' => 5, 'foo' => 'hello');
$arr[] = array('id' => 7, 'foo' => 'byebye');
$arr[] = array('id' => 9, 'foo' => 'foo');
print_r(shuffle_assoc($arr));
print_r(shuffle_assoc($arr));
print_r(shuffle_assoc($arr));

这篇关于PHP随机洗牌数组维护重点= GT;值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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