list funktsiyasi

list funktsiyasi massiv elementlarini o'zgaruvchilarga ajratadi.

Sintaksis

phplist(mixed $var, mixed ...$vars): array

Misol

Keling, massiv elementlarini o'zgaruvchilarga ajratamiz:

php<?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<?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<?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<?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<?php
  $arr = [1, 2, 3, 4, 5];
  list(, , $c) = $arr;
?>

Natijada bizda $c o'zgaruvchisi mavjud bo'ladi:

php<?php
  echo $c; // chiqadi 3
?>

Shuningdek qarang

  • extract funktsiyasi,
    u massivni o'zgaruvchilarga ajratadi