Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Jun 2012
    Location
    Switzerland
    Posts
    22
    Thanks
    7
    Thanked 1 Time in 1 Post

    multidimensional_array_rand() - same as array_rand but for multidimensional arrays

    Hi!

    Ok, so the other day I need to randomize an array. Usually I use PHP's array_rand to archive this, however I need it for a multidimensional array, and array_rand can only deal with simple arrays returning it's keys. I needed keys and values in a multidimensional array!

    I toke a look at the PHP's website, for comments in the array_rand() function, as usually people leave snippets for most of the things you need related to a specific function.

    Well, I did find a few possible solutions in there on how to do this with multidimensional arrays, but the snippets were just insane!! They were full of code with a terrible performance impact, so I just could not make use of any of those, I even haven't test them, no way, I just move on.

    So, I like clean code, and with just the enough lines. Things are simple and people use to complicate them, and the combined side effects on CPU memory usage and performance are huge.

    OK, it was time for me to create a function that would do what I want, and in my way. And I did. I admit it toke me about 3 hours to come with the final result, and I just want to share it with you.

    In fact, we'll need two functions, because one of them will act as a callback command.

    Here we go:

    PHP Code:
    <?php

    /**
     * multidimensional_array_rand()
     * 
     * @param array $array
     * @param integer $limit
     * @return array
     */
    function multidimensional_array_rand$array$limit ) {
        
        
    uksort$array'callback_rand' );

      return 
    array_slice$array0$limittrue ); 
    }

    /**
     * callback_rand()
     * 
     * @return bool
     */
    function callback_rand() { 
      
      return 
    rand() > rand();
      
    }
    ?>
    That's it, now try it out!
    PHP Code:
    <?php

    // A multidimensional array
    $my_array = array( 153 => 'ABC'
                               
    403 => array( 'DEF' => 'Other Data' ), 
                               
    306 => 'GHI'
                               
    172 => 'JKL'
                               
    838 => 'MNO'
                               
    197 => 'PQRS' );

    // Let's rand it and limit it to 3 results
    print_rmultidimensional_array_rand$my_array) );


    // Possible output would be
    // Array ( [172] => JKL [197] => PQRS [403] => Array ( [DEF] => Other Data ) )
    ?>

    That's it. Let me know if it was useful for you.
    Last edited by BornKillaz; 07-27-2012 at 03:59 PM.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •