好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php list函数用法 - php函数

php list函数用法

在php中list函数的作用是把数组中的值赋给一些变量,这样在简单的数据操作时我们就可以利用list来实现哦,有需的可以参考一下.

注: list() 仅能用于数字索引的数组并假定数字索引从 0 开始.

例子 1. list() 例子,代码如下:

<?php    $info  =  array ( 'coffee' ,  'brown' ,  'caffeine' );    // Listing all the variables   list( $drink ,  $color ,  $power ) =  $info ;  echo   "$drink is $color and $power makes it special.n" ;    // Listing some of them   list( $drink , ,  $power ) =  $info ;  echo   "$drink has $power.n" ;    // Or let's skip to only the third one   list( , ,  $power ) =  $info ;  echo   "I need $power!n" ;    ?> 

例子 2. 使用 list() 的例子,代码如下:

<table>  <tr>    <th>Employee name</th>    <th>Salary</th>  </tr> <?php    $result  = mysql_query( "SELECT id, name, salary FROM employees" , $conn );  while  (list( $id ,  $name ,  $salary ) = mysql_fetch_row( $result )) {       echo   " <tr>n" .            "  <td><a href=" info.php?id= $id ">$name</a></td>n" .            "  <td>$salary</td>n" .            " </tr>n" ;  } ?> </table> 

警告: list() 从最右边一个参数开始赋值,如果你用单纯的变量,不用担心这一点,但是如果你用了具有索引的数组,通常你期望得到的结果和在 list() 中写的一样是从左到右的,但实际上不是,是以相反顺序赋值的.

例子 3. 在 list() 中使用数组索引,代码如下:

<?php  $info  =  array ( 'coffee' ,  'brown' ,  'caffeine' );  list( $a [0],  $a [1],  $a [2]) =  $info ;  var_dump( $a );  ?> 

产生如下输出(注意单元顺序和 list() 语法中所写的顺序的比较),代码如下:

array (3) {    [2]=>    string(8)  "caffeine"     [1]=>    string(5)  "brown"     [0]=>    string(6)  "coffee"   } 

查看更多关于php list函数用法 - php函数的详细内容...

  阅读:38次