Sorting Array PHP

Discussion in 'Programming' started by apartemen, Sep 25, 2014.

  1. apartemen

    apartemen New Member

    Joined:
    Sep 25, 2014
    Messages:
    12
    Likes Received:
    2
    Trophy Points:
    3
    Di bawah adalah contoh melakukan sorting berdasar elemen array tertentu :D

    PHP:
    <?php
    // Data yang akan diurutkan, berupa array
    $data = array(
        array(
            
    'umur' => 31,
            
    'nama' => 'Anton',
            
    'email' => '[email protected]'
        
    ),
        array(
            
    'umur' => 27,
            
    'nama' => 'Andika',
            
    'email' => '[email protected]'
        
    ),
        array(
            
    'umur' => 31,
            
    'nama' => 'Amanda',
            
    'email' => '[email protected]'
        
    ),
        array(
            
    'umur' => 33,
            
    'nama' => 'Amanda',
            
    'email' => '[email protected]'
        
    ),
        array(
            
    'umur' => 17,
            
    'nama' => 'Amanda',
            
    'email' => '[email protected]'
        
    ),
        array(
            
    'umur' => 56,
            
    'nama' => 'Wisnu',
            
    'email' => '[email protected]'
        
    ),
        array(
            
    'umur' => 41,
            
    'nama' => 'Asep',
            
    'email' => '[email protected]'
        
    )
    );

    // Urutkan berdasar umur, baru nama.
    function umur_nama($a$b) {
        if (
    $a['umur'] < $b['umur']) {
            return -
    1;
        }
        if (
    $a['umur'] > $b['umur']) {
            return 
    1;
        }
        
    $i strcasecmp($a['nama'], $b['nama']);
        return 
    $i;
    }

    // Urutkan berdasar nama, baru umur.
    function nama_umur($a$b) {
        
    $i strcasecmp($a['nama'], $b['nama']);
        if (
    == $i) {
            if (
    $a['umur'] < $b['umur']) {
                return -
    1;
            }
            if (
    $a['umur'] > $b['umur']) {
                return 
    1;
            }
        }
        return 
    $i;
    }

    // Proses sorting
    $data1 $data2 $data;
    usort($data1'umur_nama');
    usort($data2'nama_umur');

    // Cek isi array
    echo '<pre>';
    print_r($data1);
    print_r($data2);
    echo 
    '</pre>';
    ?>
     
  2. Grant Verleend

    Grant Verleend Active Member

    Joined:
    Sep 10, 2014
    Messages:
    1,234
    Likes Received:
    35
    Trophy Points:
    48
    Google+:
    sip, jadi tambah referensi algoritma PHP
     
  3. Ardilas

    Ardilas Super Level

    Joined:
    Feb 18, 2013
    Messages:
    4,243
    Likes Received:
    317
    Trophy Points:
    83
    Google+:
    Kurang suka sama array, bikin pusing :D
     
  4. fairadisain

    fairadisain Member

    Joined:
    Sep 18, 2014
    Messages:
    168
    Likes Received:
    8
    Trophy Points:
    18
  5. anas44

    anas44 New Member

    Joined:
    Dec 24, 2014
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    keren banget meskipun bikin pusing tak apalah, karena beda dengan yang laen hehehe
     
  6. tonbad

    tonbad Member

    Joined:
    Dec 21, 2014
    Messages:
    260
    Likes Received:
    24
    Trophy Points:
    18
    Google+:
    salah satu seni dalam menggunakan array... :)
     
  7. kodemania

    kodemania New Member

    Joined:
    Jul 26, 2016
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    Google+:
    Array emang agak susah sih memahaminya
     

Share This Page