Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:57:12+00:00 2026-05-25T14:57:12+00:00

what is the php function to randomize the associative array while keeping key/values pairs.

  • 0

what is the php function to randomize the associative array while keeping key/values pairs. I don’t mean to just randomly pick out a key value pair, but actually changing the array (similar to the uasort function, but not in order).

TIA

example:

original array
(
    [a] => 4
    [b] => 8
    [c] => -1
    [d] => -9
    [e] => 2
    [f] => 5
    [g] => 3
    [h] => -4
)

random ordered array
(

[d] => -9
[a] => 4
[b] => 8
[c] => -1
[h] => -4   
[e] => 2
[g] => 3
[h] => -4
[f] => 5
)

Edit
Comparison between 2 solutions.

$start = microtime(true);
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
$shuffleKeys = array_keys($array);
shuffle($shuffleKeys);
$newArray = array();
foreach($shuffleKeys as $key) {
    $newArray[$key] = $array[$key];
}
print_r ($newArray);
$elapsed = microtime(true) - $start;
echo "<br>array values took $elapsed seconds.<br>";

$start = microtime(true);
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
$keys = array_keys( $array );
   shuffle( $keys );
   print_r(array_merge( array_flip( $keys ) , $array ));


$elapsed = microtime(true) - $start;
echo "<br>array values took $elapsed seconds.<br>";

Array ( [h] => -4 [e] => 2 [b] => 8 [d] => -9 [a] => 4 [c] => -1 [f] => 5 [g] => 3 )
array values took 3.0994415283203E-5 seconds.

Array ( [e] => 2 [a] => 4 [d] => -9 [c] => -1 [g] => 3 [f] => 5 [b] => 8 [h] => -4 )
array values took 4.2915344238281E-5 seconds.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:57:13+00:00Added an answer on May 25, 2026 at 2:57 pm

    You could use shuffle() on array_keys, then loop around your array adding them to the list in the new order.

    E.g.

    $shuffleKeys = array_keys($array);
    shuffle($shuffleKeys);
    $newArray = array();
    foreach($shuffleKeys as $key) {
        $newArray[$key] = $array[$key];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a function in ANSI C that would randomize an array just
<?php function toconv(string) { $gogo = array(a => b,cd => e); $string = str_replace(
<?php function data_info($data) { if ($data) { while (!feof($data)) { $buffer = fgets($data); if
The PHP function oci_connect (which connects to an Oracle database) just returns false if
Is there a PHP function that will do the following: Move a specified array
I have a PHP function that allows users to pick which categories there post
I have a PHP function that returns an array. This is the output of
This PHP function working just fine: if (preg_match (/<(\S+@\S+\w)>.*\n?.*55[0-9]/i,$body,$match)) { echo found!; } in
I have the following php function which i wrote a while ago. Now however,
I have a PHP function which populates a multi-dimensional array $client->getResponse() I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.