Omzy Posted May 9, 2009 Share Posted May 9, 2009 Lets say I have a multi-dimensional array, as such: $cats=array( 'flowers'=>array('Flowers', 'Flower Decorations'), 'balloons'=>array( 'Balloons', 'Balloon Decorations'), 'banners'=>array('Banners', 'Banner Decorations'), 'fruit-displays'=>array('Fruit Displays', 'Fruit Display Decorations'), 'ice-sculptures'=>array('Ice Sculptures', 'Ice Sculpture Decorations'), ); And currently I'm displaying this data like this: foreach($cats as $index => $value) { echo '<p><a href="/directory/'.$index.'/">'.$value[0].'</a> - '.$value[1].'</p>'; } So this just displays all the array elements in the order they are in the array. But I now need some code that will select and display any 3 elements from this array. I know I can use the shuffle() function to randomize the array but this means I lose my array indexes. Surely there is a simple way of doing this, I like to have minimal code and prefer to use PHPs in-built functions to accomplish simple tasks, rather than having to code a whole new function that does the same thing :-) Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 Take a look at array_rand() It will take the array anda number of keys to take from the array php.net's example; <?php srand((float) microtime() * 10000000); $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $rand_keys = array_rand($input, 2); echo $input[$rand_keys[0]] . "\n"; echo $input[$rand_keys[1]] . "\n"; ?> Quote Link to comment Share on other sites More sharing options...
Omzy Posted May 9, 2009 Author Share Posted May 9, 2009 I don't think I can use array_rand with a multidimensional array... Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 Have you tried it? as I beg to differ $cats=array( 'flowers'=>array('Flowers', 'Flower Decorations'), 'balloons'=>array( 'Balloons', 'Balloon Decorations'), 'banners'=>array('Banners', 'Banner Decorations'), 'fruit-displays'=>array('Fruit Displays', 'Fruit Display Decorations'), 'ice-sculptures'=>array('Ice Sculptures', 'Ice Sculpture Decorations'), ); $rand_keys = array_rand($cats, 3); foreach($rand_keys as $value) { echo '<p><a href="/directory/'.$value.'/">'.$cats[$value][0].'</a> - '.$cats[$value][1].'</p>'; } Quote Link to comment Share on other sites More sharing options...
Omzy Posted May 9, 2009 Author Share Posted May 9, 2009 Aah yes you're right, it does work! Thanks :-) BTW how come you have to do $cats[$value][0] instead of just $value[0] ? Quote Link to comment Share on other sites More sharing options...
gevans Posted May 9, 2009 Share Posted May 9, 2009 Because array_rand makes a new array, the keys are integers and its values are the keys of your original array so... $cats[$value is the original key][0] Quote Link to comment Share on other sites More sharing options...
gevans Posted June 25, 2009 Share Posted June 25, 2009 I'm not sure I understand the question, and if you do have a question you should start a new thread. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.