Contoh berbagai macam sorting array dalam PHP.
PHP Code:<?php
$buah = array('jeruk', 'apel', 'Nanas', 'semangka', 'SEMANGKA', 'Semangka', 'ANGGUR');
echo '<pre>';
echo '<h1>Asal</h1>'; print_r($buah);
echo '<h1>sort()</h1>'; $temp = $buah; sort($temp); print_r($temp);
echo '<h1>rsort()</h1>'; $temp = $buah; rsort($temp); print_r($temp);
echo '<h1>natsort()</h1>'; $temp = $buah; natsort($temp); print_r($temp);
echo '<h1>natcasesort()</h1>'; $temp = $buah; natcasesort($temp); print_r($temp);
function custom_sort($a, $b) {
return strcmp(strtolower($a), strtolower($b));
}
echo '<h1>custom sort with usort()</h1>'; $temp = $buah; usort($temp, 'custom_sort'); print_r($temp);
echo '<h1>custom sort with uasort()</h1>'; $temp = $buah; uasort($temp, 'custom_sort'); print_r($temp);
echo '</pre>'; ?>
PHP Code:<?php
$buah = array('jeruk', 'apel', 'Nanas', 'semangka', 'SEMANGKA', 'Semangka', 'ANGGUR');
echo '<pre>';
echo '<h1>Asal</h1>'; print_r($buah);
echo '<h1>sort()</h1>'; $temp = $buah; sort($temp); print_r($temp);
echo '<h1>rsort()</h1>'; $temp = $buah; rsort($temp); print_r($temp);
echo '<h1>natsort()</h1>'; $temp = $buah; natsort($temp); print_r($temp);
echo '<h1>natcasesort()</h1>'; $temp = $buah; natcasesort($temp); print_r($temp);
function custom_sort($a, $b) {
return strcmp(strtolower($a), strtolower($b));
}
echo '<h1>custom sort with usort()</h1>'; $temp = $buah; usort($temp, 'custom_sort'); print_r($temp);
echo '<h1>custom sort with uasort()</h1>'; $temp = $buah; uasort($temp, 'custom_sort'); print_r($temp);
echo '</pre>'; ?>
Tidak ada komentar:
Posting Komentar
Send Your Message