downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

natcasesort> <ksort
[edit] Last updated: Fri, 17 May 2013

view this page in

list

(PHP 4, PHP 5)

list Assegna valori a delle variabili come se fossero un array

Descrizione

void list ( mixed $... )

Come array(), questa non è in realtà una funzione, bensì un costrutto del linguaggio. list() è usata per assegnare valori ad una lista di variabili in una sola operazione.

Nota:

list() funziona solo su array numerici e si aspetta che gli indici numerici partano da 0.

Example #1 esempio di list()

<?php

$info 
= array('caffè''scuro''caffeina');

// assegna a tutte le variabili
list($bevanda$colore$componente) = $info;
echo 
"Il $bevanda è $colore e la $componente lo rende speciale.\n";

// assegna solo in parte
list($bevanda, , $componente) = $info;
echo 
"Il $bevanda ha la $componente.\n";

// oppure assegnamo solo l'ultima variabile
list( , , $componente) = $info;
echo
"Ho voglia di $bevanda!\n";

?>

Example #2 Esempio di uso di list()

<table>
 <tr>
  <th>Nome dell'impiegato</th>
  <th>Stipendio</th>
 </tr>

<?php

$risultato 
mysql_query("SELECT id, nome, stipendio FROM impiegati"$conn);
while (list(
$id$nome$stipendio) = mysql_fetch_row ($risultato)) {
    echo (
" <tr>\n".
          
"  <td><a href=\"info.php?id=$id\">$nome</a></td>\n".
          
"  <td>$stipendio</td>\n".
          
" </tr>\n");
}

?>

</table>

Avviso

list() assegna i valori cominciando dal parametro più a destra. Se si stanno usando variabili semplici, non ci si deve preoccupare di questo fatto. Ma se si stanno usando array con indici di solito ci si aspetta che l'ordine degli indici negli array sia quello scritto negli argomenti della funzione list(), da sinistra a destra; non è così. L'ordine è invertito.

Example #3 Utilizzo di list() con gli indici

<?php

$info 
= array('caffè''nero''caffeina');

list(
$a[0], $a[1], $a[2]) = $info;

var_dump($a);

?>

Restituisce il segente risultato (si noti l'ordine degli elementi rispetto all'ordine con cui sono stati scritti nella sintassi di list()).

array(3) {
  [2]=>
  string(8) "caffeina"
  [1]=>
  string(4) "nero"
  [0]=>
  string(5) "caffè"
}

Vedere anche each() e array() e extract().



natcasesort> <ksort
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes list - [4 notes]
up
0
Matt
6 hours ago
You can't type check within the list() parameters:

list ( array $var1, $var2 ) = array ( array('one','two'), 'three');

generates a parse error, unexpected 'array'.
up
16
chris at chlab dot ch
6 months ago
The example states the following:
<?php
// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar);
// output: NULL
?>

If the string is in a variable however, it seems using list() will treat the string as an array:
<?php
$string
= "abcde";
list(
$foo) = $string;
var_dump($foo);
// output: string(1) "a"
?>
up
10
svennd
5 months ago
The list() definition won't throw an error if your array is longer then defined list.
<?php

list($a, $b, $c) = array("a", "b", "c", "d");

var_dump($a); // a
var_dump($b); // b
var_dump($c); // c
?>
up
2
srikanth at networthindia dot com
3 months ago
Note: list cannot assign array cast of object to variables straight away. first you need to convert the object to numeric indexed array.

ex:
list($a, $b, $d) = (array) $abc; // $abc is an object; this will not assign.
list($a, $b, $c) = array_values((array) $abc); // This will work.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites