Page 1 of 1

Help needed for Shuffle multidimensional array

Posted: Sat Jun 06, 2009 11:46 pm
by ChrisK
I wanted to shuffle/randomize the multidimensional array before the content being displayed. Can you help me with this?

Code: Select all

<?php
 
 $news = array(
        array('item_title'=>'cnn', 'item_name'=>'news network'),
        array('item_title'=>'espn', 'item_name'=>'sports network'),
        array('item_title'=>'hbo', 'item_name'=>'movie network')
        );
 
/*** How to shuffle/randomize the above multidimensional array as i dont want display the elements in the same order 
cnn-news network, espn-sports network, hbo-movie network ***/
 
 /*** loop over the array ***/
 foreach( $news as $item )
 {
        echo $item['item_title'].'-'.$item['item_name'].'<br />';
 }
 
?>
 
Appeciate your help.

Thanks
Chris

Re: Help needed for Shuffle multidimensional array

Posted: Sat Jun 06, 2009 11:56 pm
by requinix
It's funny that you use the term shuffle.

Re: Help needed for Shuffle multidimensional array

Posted: Sun Jun 07, 2009 12:21 am
by ChrisK
tasairis: -> i tried using the shuffle like this.

$news = shuffle($news);

before the foreach loop.

but i get the following error message:

Warning: Invalid argument supplied for foreach() in /home/public_html/dev/test.php

So i am not sure shuffle() can be used in a multidimensioanl array. By the way, i am a beginner in PHP.

Re: Help needed for Shuffle multidimensional array

Posted: Sun Jun 07, 2009 12:31 am
by requinix
Did you look at that page? Especially the part that mentions "return value"?