将数组值作为函数参数传递


passing array value as parameter in function

我想把从数据库中获取的值数组作为参数传递给php函数。如果我回显这个值,它不会显示它。

      <?php
  include "config.php";
  $sql="select * from project where comp_id='1'";
  $sql1=mysql_query($sql);
  $rows=array();
  while( $fet=mysql_fetch_array($sql1))
  {
   $rows[]=$fet;
   }
  echo fun_parameter($rows);
  function fun_parameter($rows)
   {
     echo" rows". $rows['start_date']."values";   //  not working
   }
    foreach($rows as $row)
     {
     echo $name=$row['start_date'];   /working
     } 
   ?>

简单的循环可以给出结果,

 function fun_parameter($rows)
   {
     foreach($rows as $row)
     {
        echo $row['p_id'];   /working
    echo" rows". $row['start_date']."values";
     } 
     //echo" rows". $rows['start_date']."values";   //  not working
   }

如果你想从多维数组中获取特定的列,请查看php

中的array_column()

在您的代码中,您试图在没有指定索引的情况下回显数组$row,您需要循环该数组,因为它有多个值,使用以下代码:

<?php
  include "config.php";
  $sql="select * from project where comp_id='1'";
  $sql1=mysql_query($sql);
  $rows=array();
  while( $fet=mysql_fetch_array($sql1))
  {
   $rows[$i]['start_date']=$fet['start_date'];
   }
  echo fun_parameter($rows);
  function fun_parameter($rows)
   {
   foreach($rows as $row)
     {
    echo" rows". $row['start_date']."values";   //  use loop it will work
     }
   }
    foreach($rows as $row)
     {
     echo $name=$row['start_date'];   // i left this foreach here because you had it in your code    
     }