带有foreach循环的PHP多维数组,以随机顺序显示15个值中的9个

带有foreach循环的PHP多维数组,以随机顺序显示15个值中的9个,php,Php,我有一个带有foreach循环的多维数组,它以随机顺序显示15个值中的9个值 //数组值 $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/'), arr

我有一个带有foreach循环的多维数组,它以随机顺序显示15个值中的9个值
//数组值

$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
赞助商=阵列(
array(‘facebook’,‘世界上最大的社交网络’,’http://www.facebook.com/'),
array(‘adobe’,‘目标领先的软件开发人员’,’http://www.adobe.com/'),
array(‘microsoft’,‘顶级软件公司之一’,’http://www.microsoft.com/'),
阵列(“索尼”,“全球数十亿电子产品”http://www.sony.com/'),
数组('dell','enter code here'最大的计算机'之一,'http://www.dell.com/'),
array(‘ebay’,‘最大的在线拍卖和购物网站’,’http://www.ebay.com/'),
array(‘digg’,‘最流行的Web2.0社交网络之一’,’http://www.digg.com/'),
array(‘谷歌’,‘重新定义网络搜索的公司’,’http://www.google.com/'),
array(‘ea’,‘最大的电脑游戏制造商’,’http://www.ea.com/'),
数组('mysql','最流行的开源数据库引擎','http://www.mysql.com/'),
阵列(‘hp’,‘最大的计算机制造商之一’,’http://www.hp.com/'),
array(‘雅虎’,‘最受欢迎的社交媒体门户网络’,’http://www.yahoo.com/'),
阵列(“思科”,“最大的网络和通信技术”http://www.cisco.com/'),
array(‘vimeo’,‘一个流行的以视频为中心的社交网站’,’http://www.vimeo.com/'),
阵列(‘佳能’,‘成像和光学技术制造商’,'http://www.canon.com/'));
//改变价值观
洗牌(赞助商);
//处理所有值
foreach($赞助商作为$公司)
{
if(isset($company[0])&&!in_数组($company[0],$sponsors))
{//只打印15个值中的9个

对于($j=1;$j使用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>
      ';
}
$sponsors=array\u slice($sponsors,0,9);
foreach($赞助商作为$公司)
{
回声'
“.$公司[1]。”
';
}
array\u 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>
          ';
}
赞助商=阵列(
array(‘facebook’,‘世界上最大的社交网络’,’http://www.facebook.com/'),
array(‘adobe’,‘目标领先的软件开发人员’,’http://www.adobe.com/'),
array(‘microsoft’,‘顶级软件公司之一’,’http://www.microsoft.com/'),
阵列(“索尼”,“全球数十亿电子产品”http://www.sony.com/'),
数组('dell','enter code here'最大的计算机'之一,'http://www.dell.com/'),
array(‘ebay’,‘最大的在线拍卖和购物网站’,’http://www.ebay.com/'),
array(‘digg’,‘最流行的Web2.0社交网络之一’,’http://www.digg.com/'),
array(‘谷歌’,‘重新定义网络搜索的公司’,’http://www.google.com/'),
array(‘ea’,‘最大的电脑游戏制造商’,’http://www.ea.com/'),
数组('mysql','最流行的开源数据库引擎','http://www.mysql.com/'),
阵列(‘hp’,‘最大的计算机制造商之一’,’http://www.hp.com/'),
array(‘雅虎’,‘最受欢迎的社交媒体门户网络’,’http://www.yahoo.com/'),
阵列(“思科”,“最大的网络和通信技术”http://www.cisco.com/'),
array(‘vimeo’,‘一个流行的以视频为中心的社交网站’,’http://www.vimeo.com/'),
阵列(“佳能”,“成像和光学技术制造商”。”http://www.canon.com/')
);
对于($i=0;$i<9;$i++){
$company=阵列拼接($赞助商,阵列拼接($赞助商),1);
回声'
“.$公司[1]。”
';
}
用于获取前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
foreach(数组_切片($赞助商,0,9)作为$company){
回声'
“.$公司[1]。”
';
}//每一个的结束
使用或

注意:两种功能都有细微差别。因此,请查看手册并根据需要进行选择


祝你好运

请帮我解决这个问题。我只想一次打印15个值中的9个值。