PHP : Menggunakan fungsi shuffle dalam perulangan

$arr = array(
    '2012'      => array(
        array(
            'no'        => 1,
            'nama'      => 'satu',
            'ket'       => '...'
        ),
        array(
            'no'        => 2,
            'nama'      => 'dua',
            'ket'       => '...'
        ),
        array(
            'no'        => 3,
            'nama'      => 'tiga',
            'ket'       => '...'
        ),
    ),
    '2014'      => array(
        array(
            'no'        => 11,
            'nama'      => 'sebelas',
            'ket'       => '...'
        ),
        array(
            'no'        => 12,
            'nama'      => 'dua belas',
            'ket'       => '...'
        ),
        array(
            'no'        => 13,
            'nama'      => 'tiga belas',
            'ket'       => '...'
        ),
    )
);

foreach( $arr as $k => $a )
    shuffle( $arr[ $k ] );

print_r( $arr );

Hasil

Array
(
    [2012] => Array
        (
            [0] => Array
                (
                    [no] => 3
                    [nama] => tiga
                    [ket] => ...
                )

            [1] => Array
                (
                    [no] => 1
                    [nama] => satu
                    [ket] => ...
                )

            [2] => Array
                (
                    [no] => 2
                    [nama] => dua
                    [ket] => ...
                )

        )

    [2014] => Array
        (
            [0] => Array
                (
                    [no] => 12
                    [nama] => dua belas
                    [ket] => ...
                )

            [2] => Array
                (
                    [no] => 13
                    [nama] => tiga belas
                    [ket] => ...
                )

            [3] => Array
                (
                    [no] => 11
                    [nama] => sebelas
                    [ket] => ...
                )

        )

)

Tinggalkan Balasan