Powered by Blogger.

How to shuffle PHP array with a key?


In some cases, you want to make the array with a random order so when users open a page it may see different contents. The following content tells you how to make that. In fact, it is about how to shuffle PHP array with a key.

The codes as the following:

function wpprorgramming_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; 
    }


No comments