PHP Random Shuffle Array Maintaining Key => Value

后端 未结 10 1100
轮回少年
轮回少年 2020-11-27 19:21

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).

相关标签:
10条回答
  • 2020-11-27 19:49
      $testArray = array('a' => 'apple', 'b' => 'ball', 'c' => 'cat', 'd' => 'dog');
      $keys = array_keys($testArray); //Get the Keys of the array -> a, b, c, d
      shuffle($keys); //Shuffle The keys array -> d, a, c, b
      $shuffledArray = array();
      foreach($keys as $key) {
        $shuffledArray[$key] = $testArray[$key]; //Get the original array using keys from shuffled array
      }
      print_r($shuffledArray);
      /*
      Array
      (
          [d] => dog
          [a] => apple
          [c] => cat
          [b] => ball
      )
      */
    
    0 讨论(0)
  • 2020-11-27 19:51

    I was having a hard time with most of the answers provided - so I created this little snippet that took my arrays and randomized them while maintaining their keys:

    function assoc_array_shuffle($array)
    {
        $orig = array_flip($array);
        shuffle($array);
        foreach($array AS $key=>$n)
        {
            $data[$n] = $orig[$n];
        }
        return array_flip($data);
    }
    
    0 讨论(0)
  • 2020-11-27 19:52

    I tried the most vote solution didn't popular shuffle list. This is the change I made to make it work. I want my array key starting from 1.

     $list = array_combine(range(1,10),range(100,110));
     $shuffle_list =  shuffle_assoc($list);
    
    
    function shuffle_assoc($list)
    {
        if (!is_array($list)) return $list;
    
        $keys = array_keys($list);
        shuffle($list);
        $random = array();
        foreach ($keys as $k => $key) {
            $random[$key] = $list[$k];
        }
        return $random;
    }
    
    0 讨论(0)
  • 2020-11-27 19:53

    Take a look to this function here :

         $foo = array('A','B','C'); 
    
    
    function shuffle_with_keys(&$array) {
        /* Auxiliary array to hold the new order */
        $aux = array();
        /* We work with an array of the keys */
        $keys = array_keys($array);
        /* We shuffle the keys */`enter code here`
        shuffle($keys);
        /* We iterate thru' the new order of the keys */
        foreach($keys as $key) {
          /* We insert the key, value pair in its new order */
          $aux[$key] = $array[$key];
          /* We remove the element from the old array to save memory */
          unset($array[$key]);
        }
        /* The auxiliary array with the new order overwrites the old variable */
        $array = $aux;
      }
    
          shuffle_with_keys($foo);
          var_dump($foo);
    

    Original post here : http://us3.php.net/manual/en/function.shuffle.php#83007

    0 讨论(0)
  • 2020-11-27 19:56
    function shuffle_assoc($array)
    {
        $keys = array_keys($array);
        shuffle($keys);
        return array_merge(array_flip($keys), $array);
    }
    
    0 讨论(0)
  • 2020-11-27 20:00

    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));
    
    0 讨论(0)
提交回复
热议问题