PHP 5
PHP tartiblash funksiyalari

PHP - Jadvallar uchun saralash funktsiyalari

Ushbu bobda quyidagi PHP qatori funktsiyalarini bajaramiz:

  • sort() - ketma-ketlikni tartibda tartiblashtirish
  • rsort() - tartiblangan qatorlarni kam tartib bilan ajratish
  • asort() - qiymatiga qarab ortib boruvchi tartibda assotsiativ qatorlar
  • ksort() - kaliti bo'yicha ortib boruvchi tartibda assotsiativ qatorlar
  • arsort() - qiymati bo'yicha arsort() tartibda assotsiativ qatorlarni ajratish
  • krsort() - krsort() tartibda assotsiativ qatorlarni krsort()

Tartiblash tartibini ketma-ketlikda tartiblash - sort ()

Quyidagi misol, $ aravalarining qatorlarini alifbo tartibidagi tartibda tartiblaydi:

Misol


<?php

$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>

Quyidagi misol $ raqamlar qatorini elementlari ortib boruvchi soni tartibida tartiblaydi:

Misol


<?php

$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>


Tartiblash qatori bilan tartiblash - rsort ()

Quyidagi misolda $ aravalarining qatorlari alfavit tartibida kamayib boradi:

Misol


<?php

$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>

Quyidagi misol $ raqamlar qatori elementlarini kamayib boradigan soni tartibida tartiblaydi:

Misol


<?php

$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>


Sortni Array (Ortgan Buyurtma), Qiymati bo'yicha - asort ()

Quyidagi misol, qiymati bo'yicha ortib boruvchi tartibda assotsiativ qatorni tartiblaydi:

Misol


<?php

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

asort($age);
?>


Tartiblash qatori (Ortgan tartib), Key - ksort ()

Quyidagi misol kalitlarga muvofiq ortib borayotgan tartibda assotsiativ qatorni tartiblaydi:

Misol


<?php

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

ksort($age);
?>


Sortni Array (A tartibida tartiblash), Value - arsort ()

Quyidagi misol, assotsiativ qatorni kamida navbatga qarab tartiblaydi:

Misol


<?php

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

arsort($age);
?>


Sortni Array (A tartibida tartiblash), Key - krsort ()

Quyidagi misol kalitlarga muvofiq assotsiativ qatorni kamida tartiblaydi:

Misol


<?php

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

krsort($age);
?>

Site is in test mode