2012-07-30 146 views
0

僅示出9個值的15我有foreach循環一個多維陣列中,其示出了在隨機順序僅有9的15個值
//數組值PHP-多維陣列foreach循環和隨機順序

$sponsors = array(
    array('facebook','The biggest social network in the world.','http://www.facebook.com/'), 
    array('adobe','The leading software developer targeted','http://www.adobe.com/'), 
    array('microsoft','One of the top software companies','http://www.microsoft.com/'), 
    array('sony','A global multibillion electronics','http://www.sony.com/'), 
    array('dell','One of`enter code here` the biggest computer','http://www.dell.com/'), 
    array('ebay','The biggest online auction and shopping websites.','http://www.ebay.com/'), 
    array('digg','One of the most popular web 2.0 social networks.','http://www.digg.com/'), 
    array('google','The company that redefined web search.','http://www.google.com/'), 
    array('ea','The biggest computer game manufacturer.','http://www.ea.com/'), 
    array('mysql','The most popular open source database engine.','http://www.mysql.com/'), 
    array('hp','One of the biggest computer manufacturers.','http://www.hp.com/'), 
    array('yahoo','The most popular network of social media portals','http://www.yahoo.com/'), 
    array('cisco','The biggest networking and communications techno','http://www.cisco.com/'), 
    array('vimeo','A popular video-centric social networking site.','http://www.vimeo.com/'), 
    array('canon','Imaging and optical technology manu facturer.','http://www.canon.com/')); 

//Shuffling the values 
shuffle($sponsors); 

//To process all values 
foreach($sponsors as $company) 
{ 
    if(isset($company[0]) && !in_array($company[0],$sponsors)) 
    {//to print only 9 values out of 15 
     for($j=1; $j<=9; $j++) 
     { 
     echo' 
      <div class="sponsor" title="Click to flip"> 
      <div class="sponsorFlip"> 
      <img src="img/sponsors/'.$company[0].'.png" alt="More about '.$company[0].'" /> 
      </div> 
      <div class="sponsorData"> 
      <div class="sponsorDescription">'.$company[1].'</div> 
      <div class="sponsorURL"><a href="'.$company[2].'">'.$company[2].'</a> 
      </div> 
      </div> 
      </div> 
      '; 
     } 
     } 
}//end of for each 
+0

請幫我看看這個..我希望一次只打印9個值中的15個值。 – 2012-07-30 10:38:16

回答

0

使用array_slice從混洗陣列中選擇前9個值。

$sponsors = array_slice($sponsors, 0, 9); 

foreach($sponsors as $company) 
{ 
    echo' 
     <div class="sponsor" title="Click to flip"> 
     <div class="sponsorFlip"> 
     <img src="img/sponsors/'.$company[0].'.png" alt="More about '.$company[0].'" /> 
     </div> 
     <div class="sponsorData"> 
     <div class="sponsorDescription">'.$company[1].'</div> 
     <div class="sponsorURL"><a href="'.$company[2].'">'.$company[2].'</a> 
     </div> 
     </div> 
     </div> 
     '; 
} 
0

array_rand()是最簡單和最巧妙的方法,我覺得:

$sponsors = array(
    array('facebook','The biggest social network in the world.','http://www.facebook.com/'), 
    array('adobe','The leading software developer targeted','http://www.adobe.com/'), 
    array('microsoft','One of the top software companies','http://www.microsoft.com/'), 
    array('sony','A global multibillion electronics','http://www.sony.com/'), 
    array('dell','One of`enter code here` the biggest computer','http://www.dell.com/'), 
    array('ebay','The biggest online auction and shopping websites.','http://www.ebay.com/'), 
    array('digg','One of the most popular web 2.0 social networks.','http://www.digg.com/'), 
    array('google','The company that redefined web search.','http://www.google.com/'), 
    array('ea','The biggest computer game manufacturer.','http://www.ea.com/'), 
    array('mysql','The most popular open source database engine.','http://www.mysql.com/'), 
    array('hp','One of the biggest computer manufacturers.','http://www.hp.com/'), 
    array('yahoo','The most popular network of social media portals','http://www.yahoo.com/'), 
    array('cisco','The biggest networking and communications techno','http://www.cisco.com/'), 
    array('vimeo','A popular video-centric social networking site.','http://www.vimeo.com/'), 
    array('canon','Imaging and optical technology manufacturer.','http://www.canon.com/') 
); 

for ($i = 0; $i < 9; $i++) { 
    $company = array_splice($sponsors, array_rand($sponsors), 1); 
    echo' 
      <div class="sponsor" title="Click to flip"> 
      <div class="sponsorFlip"> 
      <img src="img/sponsors/'.$company[0].'.png" alt="More about '.$company[0].'" /> 
      </div> 
      <div class="sponsorData"> 
      <div class="sponsorDescription">'.$company[1].'</div> 
      <div class="sponsorURL"><a href="'.$company[2].'">'.$company[2].'</a> 
      </div> 
      </div> 
      </div> 
      '; 
} 
1

使用array_slice拿到第一個9個元素。

foreach (array_slice($sponsors, 0, 9) as $company) { 
    echo' 
    <div class="sponsor" title="Click to flip"> 
    <div class="sponsorFlip"> 
    <img src="img/sponsors/'.$company[0].'.png" alt="More about '.$company[0].'" /> 
    </div> 
    <div class="sponsorData"> 
    <div class="sponsorDescription">'.$company[1].'</div> 
    <div class="sponsorURL"><a href="'.$company[2].'">'.$company[2].'</a> 
    </div> 
    </div> 
    </div> 
    '; 
}//end of for each 
0

使用array_slicearray_splice

注意:這兩個功能有輕微的差異。所以看看手冊,並根據您的需要選擇。

運氣好的