list funktsiyasi
list funktsiyasi massiv elementlarini
o'zgaruvchilarga ajratadi.
Sintaksis
list(mixed $var, mixed ...$vars): array
Misol
Keling, massiv elementlarini o'zgaruvchilarga ajratamiz:
<?php
$arr = [1, 2, 3, 4, 5];
list($a, $b, $c, $d, $e) = $arr;
?>
Natijada bizda $a o'zgaruvchisi,
$b o'zgaruvchisi,
$c o'zgaruvchisi,
$d o'zgaruvchisi va
$e o'zgaruvchisi mavjud bo'ladi:
<?php
echo $a; // chiqadi 1
echo $b; // chiqadi 2
echo $c; // chiqadi 3
echo $d; // chiqadi 4
echo $e; // chiqadi 5
?>
Misol
Agar massivning birinchi elementidan emas, boshqasidan boshlash kerak bo'lsa - vergul qo'yish mumkin:
<?php
$arr = [1, 2, 3, 4, 5];
list(, $b, $c, $d, $e) = $arr;
?>
Natijada bizda $b o'zgaruvchisi,
$c o'zgaruvchisi,
$d o'zgaruvchisi va
$e o'zgaruvchisi mavjud bo'ladi:
<?php
echo $b; // chiqadi 2
echo $c; // chiqadi 3
echo $d; // chiqadi 4
echo $e; // chiqadi 5
?>
Misol
Massivning faqat uchinchi elementini o'zgaruvchiga yozamiz:
<?php
$arr = [1, 2, 3, 4, 5];
list(, , $c) = $arr;
?>
Natijada bizda
$c o'zgaruvchisi mavjud bo'ladi:
<?php
echo $c; // chiqadi 3
?>
Shuningdek qarang
-
extractfunktsiyasi,
u massivni o'zgaruvchilarga ajratadi