如何在函数中使用foreach循环?

时间:2016-06-10 05:25:40

标签: php function foreach

我有一个功能:

   function animals($a,$b,$c,$d){
        $pdo = Database::connect(); 
        $sql = 'SELECT  *  FROM animals ORDER BY id DESC';
        foreach ($pdo->query($sql) as $row) {
            echo "<td>".$a."</td>"; 
            echo "<td>".$b."</td>"; 
            echo "<td>".$c."</td>"; 
            echo "<td>".$d."</td>"; 
        }   
    }

我的问题是,我需要使用不同数量的参数多次使用该函数。这意味着在我的foreach循环中,有时会有4个项目...

 animals($row['a'],$row['b'],$row['c'],$row['d'],);

......有时会有6件......

  animals($row['e'],$row['f'],$row['g'],$row['h'],$row['i'],$row['j']);

...或有时候3 ...

animals($row['k'],$row['l'],$row['m']);

......等等。所以我不知道如何告诉我的功能,我的foreach项目的数量有所不同

更具体地说,如果我不使用某个功能,我的页面将如下所示:

     $pdo = Database::connect(); 
            $sql = 'SELECT  *  FROM animals ORDER BY id DESC';

    <div>

      <!-- Nav tabs -->
      <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
        <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
        <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
        <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
      </ul>

      <!-- Tab panes -->
      <div class="tab-content">
        <div role="tabpanel" class="tab-pane active" id="home">
<?php
           foreach ($pdo->query($sql) as $row) {
                echo "<td>".$a."</td>"; 
                echo "<td>".$b."</td>"; 
                echo "<td>".$c."</td>"; 
                echo "<td>".$d."</td>"; 
            } 

?>  
    </div>
        <div role="tabpanel" class="tab-pane" id="profile">
<?php
           foreach ($pdo->query($sql) as $row) {
                echo "<td>".$row['a']."</td>"; 
                echo "<td>".$row['b']."</td>"; 
                echo "<td>".$row['c']."</td>"; 
                echo "<td>".$row['d']."</td>"; 
            } 

?>  
    </div>
        <div role="tabpanel" class="tab-pane" id="messages">
<?php
           foreach ($pdo->query($sql) as $row) {
                echo "<td>".$row['e']."</td>"; 
                echo "<td>".$row['f']."</td>"; 
                echo "<td>".$row['g']."</td>"; 
                echo "<td>".$row['h']."</td>"; 
            } 

?>  
    </div>
        <div role="tabpanel" class="tab-pane" id="settings">
<?php
           foreach ($pdo->query($sql) as $row) {
                echo "<td>".$row['i']."</td>"; 
                echo "<td>".$row['j']."</td>";  
            } 

?>  
    </div>
      </div>

    </div> 

3 个答案:

答案 0 :(得分:2)

您可以使用func_get_args()获取函数参数数组。

function animals(){
  $args = func_get_args();

  $pdo = Database::connect(); 
  $sql = 'SELECT  *  FROM animals ORDER BY id DESC';

  foreach ($pdo->query($sql) as $row) {
    foreach ($args as $arg_value) {
      echo "<td>".$arg_value."</td>"; 
    }
  }
}

// Call function
animals(1, 2, 3);
// or 
animals(1, 2, 3, 4);

修改

我想您只想打印$row的某些字段?

function animals(){
  $args = func_get_args();

  foreach ($args as $arg_value) {
    echo "<td>".$arg_value."</td>"; 
  }
}

$pdo = Database::connect(); 
$sql = 'SELECT * FROM `animals` ORDER BY id DESC';

foreach ($pdo->query($sql) as $row) {
  animals($row['a'], $row['b'], $row['c']);
  // or
  animals($row['i'], $row['j']);
}

更短:

function animals(){
  $args = func_get_args();

  $pdo = Database::connect(); 
  $sql = 'SELECT `'.implode('`, `', $args).'` FROM `animals` ORDER BY id DESC';

  foreach ($pdo->query($sql) as $row) {
    foreach ($row as $value) {
      echo "<td>".$value."</td>"; 
    }
  }
}

// Then you can just do
animals('a', 'b', 'c');

// or
animals('i', 'j');

答案 1 :(得分:1)

为了实现这种功能,您需要创建一个没有参数的函数,并且需要使用PHP函数func_get_args()

function animals(){
   $arr = func_get_args();
   foreach($arr as $v){
      //
   }
}

animals(1,2,3,4,5);
animals(1,2,3);

Demo

答案 2 :(得分:1)

只需将完整的$row数组传递给您的函数,然后像

一样循环
function animals($val){
   $pdo = Database::connect(); 
   $sql = 'SELECT  *  FROM animals ORDER BY id DESC';
   foreach ($pdo->query($sql) as $row) {
     foreach($val as $v){
       echo "<td>".$v."</td>";
     }
   }   
}

然后像animals($row);

一样调用您的函数

另外,如果我没有错,你不需要再次查询你的函数,只需保持简单,如

function animals($val){
    foreach($val as $v){
       echo "<td>".$v."</td>";
     }
}