poe Posted May 1, 2007 Share Posted May 1, 2007 i have 3 arrays of links (payed, localad, personal). these are for banners i want to show on my site. payed = these links, people payed for localad = these are my own banner ads which advertise 'buy banner today' personal = these are to my personal links (my wife isnt going to pay for an ad space on my website! ) $payed = array( array( 'url'=>'www.link1.com', 'img'=>'img1.jpg' ), array( 'url'=>'www.link2.com', 'img'=>'img2.jpg' ), array( 'url'=>'www.link3.com', 'img'=>'img3.jpg' ), array( 'url'=>'www.link4.com', 'img'=>'img4.jpg' ), ); $localad = array( array( 'url'=>'advertise.html', 'img'=>'buynow.jpg' ), array( 'url'=>'advertise.html', 'img'=>'info.jpg' ), array( 'url'=>'advertise.html', 'img'=>'clickhere.jpg' ), ); $personal = array( array( 'url'=>'myblog.com', 'img'=>'my.jpg' ), array( 'url'=>'wifeblog.com', 'img'=>'wife.jpg' ), array( 'url'=>'myfriendblog.com', 'img'=>'myfriend.jpg' ), array( 'url'=>'wifefriendblog.com', 'img'=>'wifefriend.jpg' ), ); i have another array which shows the type of banner to show, depending on what page you are on. eg, some pages i have set to show 3 banners, some pages i have set to show, 4 banners, or 5 banners. etc.. (goes from 1 to 5) $level = array( array( 'show'=>1, 'payed'=>1, 'localad'=>0, 'personal'=>0, ), array( 'show'=>2, 'payed'=>1, 'localad'=>1, 'personal'=>0, ), array( 'show'=>3, 'payed'=>2, 'localad'=>1, 'personal'=>0, ), array( 'show'=>4, 'payed'=>3, 'localad'=>1, 'personal'=>0, ), array( 'show'=>5, 'payed'=>3, 'localad'=>1, 'personal'=>1, ), ); eg.. $level[2]; is equivalent to: array( 'show'=>3, 'payed'=>2, 'localad'=>1, 'personal'=>0, ), means 2 random payed ads are shown + 1 localad = 3 total ads shown and $level[4]; is equivalent to: array( 'show'=>5, 'payed'=>3, 'localad'=>1, 'personal'=>1, ), means 3 payed ads shown + 1 localad + 1 personal = 5 total ads shown etc.... how do i grab the ads from their respective arrays? is there a way to condense things so that i can loop trough my code and depending on the $level array 'key' that has value greater than 0. grab the links from that array since they have the same name? right now i have: (assuming i am looking for 3 ads) $rand_payed[] = array_rand( $payed, level[2][payed] ); $rand_localad[] = array_rand( $localad, level[2][localad] ); $rand_personal[] = array_rand( $personal, level[2][persoanl] ); now im stuck!!! thanks chris Link to comment https://forums.phpfreaks.com/topic/49540-display-random-results-from-multiple-arrays/ Share on other sites More sharing options...
genericnumber1 Posted May 1, 2007 Share Posted May 1, 2007 I HOPE this is what you meant... <?php $linkSettings = array ( 'payed' => array( array( 'url' => 'www.link1.com', 'img' => 'img1.jpg' ), array( 'url' => 'www.link2.com', 'img' => 'img2.jpg' ), array( 'url' => 'www.link3.com', 'img' => 'img3.jpg' ), array( 'url' => 'www.link4.com', 'img' => 'img4.jpg' ), ), 'localad' => array( array( 'url' => 'advertise.html', 'img' => 'buynow.jpg' ), array( 'url' => 'advertise.html', 'img' => 'info.jpg' ), array( 'url' => 'advertise.html', 'img' => 'clickhere.jpg' ), ), 'personal' => array( array( 'url' => 'myblog.com', 'img' => 'my.jpg' ), array( 'url' => 'wifeblog.com', 'img' => 'wife.jpg' ), array( 'url' => 'myfriendblog.com', 'img' => 'myfriend.jpg' ), array( 'url' => 'wifefriendblog.com', 'img' => 'wifefriend.jpg' ), ), ); $levelSettings = array( 1 => array( 'payed' => 1, 'localad' => 0, 'personal' => 0, ), 2 => array( 'payed' => 1, 'localad' => 1, 'personal' => 0, ), 3 => array( 'payed' => 2, 'localad' => 1, 'personal' => 0, ), 4 => array( 'payed' => 3, 'localad' => 1, 'personal' => 0, ), 5 => array( 'payed' => 3, 'localad' => 1, 'personal' => 1, ), ); $level = 4; // Set a level to test the script $linksArray = array(); foreach($levelSettings[$level] as $linkType => $numberOf) { // Make sure $numberOf isn't <= 0 if($numberOf > 0) { // Make sure number requested does not exceed size of array if($numberOf > count($linkSettings[$linkType])) { $numberOf = count($linkSettings[$linkType]); } // Get random keys $keys = array_rand($linkSettings[$linkType], $numberOf); // Save the items that go with the random keys foreach((array)$keys as $key) { $linksArray[] = $linkSettings[$linkType][$key]; } } } // Maybe shuffle the array to make the items not be grouped by type? shuffle($linksArray); print_r($linksArray); ?> Link to comment https://forums.phpfreaks.com/topic/49540-display-random-results-from-multiple-arrays/#findComment-242852 Share on other sites More sharing options...
Barand Posted May 1, 2007 Share Posted May 1, 2007 <?php shuffle($payed); shuffle($localad); shuffle($personal); /** * having determined you need $x paid ads, $y local and $z personal ads */ $rand_payed = array_slice($payed, 0, $x); $rand_local = array_slice($localads, 0, $y); $rand_personal = array_slice($personal, 0, $z); ?> Link to comment https://forums.phpfreaks.com/topic/49540-display-random-results-from-multiple-arrays/#findComment-242858 Share on other sites More sharing options...
genericnumber1 Posted May 1, 2007 Share Posted May 1, 2007 <?php shuffle($payed); shuffle($localad); shuffle($personal); /** * having determined you need $x paid ads, $y local and $z personal ads */ $rand_payed = array_slice($payed, 0, $x); $rand_local = array_slice($localads, 0, $y); $rand_personal = array_slice($personal, 0, $z); ?> You and your overdeveloped sense of simple solutions must you beat me in speed and readability so often?... it hurts my confidence! Link to comment https://forums.phpfreaks.com/topic/49540-display-random-results-from-multiple-arrays/#findComment-242862 Share on other sites More sharing options...
Barand Posted May 1, 2007 Share Posted May 1, 2007 I guess I'm used to deadlines and working on fixed price jobs, so I try to keep two acronyms in mind MNC - minimum necessary code MDE - maximum desired effect Link to comment https://forums.phpfreaks.com/topic/49540-display-random-results-from-multiple-arrays/#findComment-242881 Share on other sites More sharing options...
poe Posted May 1, 2007 Author Share Posted May 1, 2007 thanks guys, you rock! i took me longer to write my Q, than for you to respond!! Link to comment https://forums.phpfreaks.com/topic/49540-display-random-results-from-multiple-arrays/#findComment-242889 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.