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

时间:2015-05-19 05:32:34

标签: php mysql

我想将从数据库中获取的值数组作为参数传递给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

     } 

   ?>

2 个答案:

答案 0 :(得分:0)

简单循环可以给出结果,

 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()

答案 1 :(得分:-1)

在您的代码中,您尝试在没有指定索引的情况下回显数组$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    
     }