|
użytkowników online: 25
|
OPINIE UŻYTKOWNIKÓW
|
Uważam, że serwis jest najlepszy na świecie. Wykonany rzetelnie, a wszystkie skrypty sa dopracowane. Zamieszczony materiał godny mistrza. Jestem programistą od wielu lat i bez tego serwisu nie istnieje. Upraszacza życie każdemu programiście. Imponujący jest fakt, że do twórcy serwisu zawsze można się zwrócić z prośbą o pomoc i uzyskuje się ją w bardzo krótkim czasie. Najważniejsze w tym wszystkim jest to, że można korzystać z witryny za symboliczną opłatą.
Marcin Kowalski Multinet Polska
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
list (PHP 3, PHP 4, PHP 5 ) list --
Przypisz zmienne tak jakby były tablicą
Opisvoid list ( mixed zmienna, mixed ... )
Podobnie jak array(), nie jest na prawdę funkcją,
ale elementem składni języka. Instrukcja list()
jest używana do przypisywania listy zmiennych w jednej operacji.
Notatka:
list() działa tylko z tablicami o indeksach
liczbowych zakładając że indeksy zaczynają się od 0.
Przykład 1. Przykłady użycia list() |
<?php
$info = array('kawa', 'brązowa', 'kofeina');
list($napój, $kolor, $składnik) = $info;
echo "$napój jest $kolor a $składnik czyni ją wyjątkową.\n";
list($napój, , $składnik) = $info;
echo "$napój zawiera $składnik.\n";
list( , , $składnik) = $info;
echo "Potrzebna jest mi $składnik!\n";
?>
|
|
Przykład 2. Przykład użycia list() |
<table>
<tr>
<th>Nazwisko pracownika</th>
<th>Pensja</th>
</tr>
<?php
$wynik = mysql_query("SELECT id, nazwisko, pensja FROM pracownicy", $conn);
while (list($id, $nazwisko, $pensja) = mysql_fetch_row($wynik)) {
echo " <tr>\n" .
" <td><a href=\"info.php?id=$id\">$nazwisko</a></td>\n" .
" <td>$pensja</td>\n" .
" </tr>\n";
}
?>
</table>
|
|
| Ostrzeżenie |
list() Przypisuje wartości zaczynając parametru
znajdującego się po prawej. Przy używaniu zwykłych zmiennych nie ma
się o co martwić. Ale używając tablic z indeksami, zazwyczaj
oczekuje się, że kolejność indeksów w tablicy będzie taka sama jak zapis
wywołania list(), od lewej do prawej. Otóż tak nie
jest. Zmienne są przypisywane w odwrotnej kolejności.
|
Przykład 3. Przykład użycia list() z indeksowaną
tablicą |
<?php
$info = array('kawa', 'brązowa', 'kofeina');
list($a[0], $a[1], $a[2]) = $info;
var_dump($a);
?>
|
|
Przykład ten wyświetli co następuje (zauważ kolejność elementów
porównując ją do tej, w której były one zapisane w wywołaniu funkcji
list()):
array(3) {
[2]=>
string(8) "kofeina"
[1]=>
string(5) "brązowa"
[0]=>
string(6) "kawa"
} |
Patrz także: each() array() i
extract().
User Contributed NotesZain
16-Jan-2006 09:23
extract is a better option.
e.g. $row = mysql_fetch_assoc($result);
and this returns
$row['firstname'];
$row['lastname'];
$row['id'];
now using extract can be handy here..........
extract($row);
and then u can use
echo $firstname;
echo $lastname;
echo $id;
i.e. array keys will become variables and keys' values the vlaues of the variables
mzizka at hotmail dot com
03-Jan-2006 05:49
Elements on the left-hand side that don't have a corresponding element on the right-hand side will be set to NULL. For example,
<?php
$y = 0;
list($x, $y) = array("x");
var_dump($x);
var_dump($y);
?>
Results in:
string(1) "x"
NULL
Nearsighted
25-Jul-2005 04:34
list, coupled with while, makes for a handy way to populate arrays.
while (list($repcnt[], $replnk[], $date[]) = mysql_fetch_row($seek0))
{
// insert what you want to do here.
}
PHP will automatically assign numerical values for the array because of the [] signs after the variable.
From here, you can access their row values by array numbers.
eg.
for ($i=0;$i<$rowcount;$i++)
{
echo "The title number $repcnt[$i] was written on $date[$i].";
}
webmaster at miningstocks dot com
01-Jun-2005 08:05
One way to use the list function with non-numerical keys is to use the array_values() function
<?php
$array = array ("value1" => "one", "value2" => "two");
list ($value1, $value2) = array_values($array);
?>
php at keithtyler dot com
12-May-2005 11:27
Note to perl programmers, this does NOT work like Perl's anonymous-list technique. It will discard any elements on the right hand side that do not have a corresponding recipient value on the left hand side.
In Perl, you can put an array at the end of the list, and it will pick up all remaining elements from the right hand of the assignment.
($val1,$val2,@others)=array("a","b","c","d","e")
will result in $val1="a", $val2="b", and @others=("c","d","e").
This cannot seem to be done in PHP, even if you declare the array beforehand:
$others=Array();
list($val1,$val2,$others)=array("a","b","c","d","e")
will result in $val1="a", $val2="b", and $others="c". Note also that $others is redefined as whatever type the right-hand side value was, and is no longer an Array.
This is in 4.3.11.
mortoray at ecircle-ag dot com
16-Feb-2005 10:29
There is no way to do reference assignment using the list function, therefore list assignment is will always be a copy assignment (which is of course not always what you want).
By example, and showing the workaround (which is to just not use list):
function &pass_refs( &$a ) {
return array( &$a );
}
$a = 1;
list( $b ) = pass_refs( $a ); //*
$a = 2;
print( "$b" ); //prints 1
$ret = pass_refs( $a );
$b =& $ret[0];
$a = 3;
print( "$b" ); //prints 3
*This is where some syntax like the following would be desired:
list( &$b ) = pass_refs( $a );
or maybe:
list( $b ) =& pass_refs( $a );
jennevdmeer at zonnet dot nl
21-Oct-2004 05:29
This is a function simulair to that of 'list' it lists an array with the 'key' as variable name and then those variables contain the value of the key in the array.
This is a bit easier then list in my opinion since you dont have to list up all variable names and it just names them as the key.
<?php
function lista($a) {
foreach ($a as $k => $v) {
$s = "global \$".$k;
eval($s.";");
$s = "\$".$k ." = \"". $v."\"";
eval($s.";");
}
}
?>
HW
14-Aug-2004 10:08
The list() construct can be used within other list() constructs (so that it can be used to extract the elements of multidimensional arrays):
<?php
$matrix = array(array(1,2),
array(3,4));
list(list($tl,$tr),list($bl,$br)) = $matrix;
echo "$tl $tr $bl $br";
?>
Outputs "1 2 3 4".
jeronimo at DELETE_THIS dot transartmedia dot com
29-Jan-2004 04:28
If you want to swap values between variables without using an intermediary, try using the list() and array() language constructs. For instance:
<?
$biggest = 1;
$smallest = 10;
$temp = $biggest;
$biggest = $smallest;
$smallest = $temp;
list($biggest, $smallest) = array($smallest, $biggest);
?>
This works with any number of variables; you're not limited to just two.
Cheers,
Jeronimo
rubein at earthlink dot net
29-Dec-2000 02:15
Note: If you have an array full of arrays, you can't use list() in conjunction to foreach() when traversing said array, e.g.
$someArray = array(
array(1, "one"),
array(2, "two"),
array(3, "three")
);
foreach($somearray as list($num, $text)) { ... }
This, however will work
foreach($somearray as $subarray) {
list($num, $text) = $subarray;
...
}
|