如果在Array PHP

时间:2015-07-04 18:22:38

标签: php arrays sorting

我有以下数组数据

0 =>
id => 1
id_requestor => 1
jam_input => 2015-07-04 08:00:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 10

1 =>
id => 2
id_requestor => 2
jam_input => 2015-07-04 06:23:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 15

2 =>
id => 3
id_requestor => 3
jam_input => 2015-07-04 07:24:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 15

3 =>
id => 4
id_requestor => 4
jam_input => 2015-07-04 05:09:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 10

在上面的数组中,有些total_poin数据具有相同的值,但值不同jam_input

我想按降序排序数组数据。我已经通过desc对它进行了排序。但是如果在jam_input中找到相同的值,我想添加total_poin值检查。然后,如果找到jam_input[i]< jam_input[i+1]i前面i+1添加数据。

这是我的分类代码:

function LJFAlgorithm($arrayParam) {

        // Ambil Data array dari database sesuai dengan waktu sekarang
        $batchData = getBatch($arrayParam);

        // Flag untuk penanda apakah array sudah tersortir
        $sorted = false;

        // Jika belum tersortir maka
        while (false === $sorted) {
            $sorted = true;

            // Looping batchData - 1
            for ($i = 0; $i < count($batchData)-1; ++$i) {

                // Data Awal
                $current = $batchData[$i]['total_poin'];

                // Data Indeks selanjutnya
                $next = $batchData[$i+1]['total_poin'];

                // Jika poin awal data > dari poin selanjutnya maka,
                if ($next > $current) {

                    // Ganti nilai data awal dengan data selanjutnya
                    $batchData[$i]['total_poin'] = $next;

                    // Ganti data selanjutnya dengan data awal
                    $batchData[$i+1]['total_poin'] = $current;

                    // Ganti status berurutan ke false 'reset ulang'
                    $sorted = false;
                } else if ($next == $current) {
                    // If found same total_poin
                    if (strtotime($batchData[$i]['jam_input']) > strtotime($batchData[$i + 1]['jam_input'])) {
                        // Ganti nilai data awal dengan data selanjutnya
                        $batchData[$i]['total_poin'] = $next;

                        // Ganti data selanjutnya dengan data awal
                        $batchData[$i+1]['total_poin'] = $current;
                    }

                }
            }
        }

        // Kembalikan array yang sudah terurut
        return $batchData;
    }

我目前的结果是这样的。

0 =>
id => 1
id_requestor => 1
jam_input => 2015-07-04 08:00:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 10

1 =>
id => 2
id_requestor => 2
jam_input => 2015-07-04 06:23:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 15

2 =>
id => 3
id_requestor => 3
jam_input => 2015-07-04 07:24:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 15

3 =>
id => 4
id_requestor => 4
jam_input => 2015-07-04 05:09:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 10

我的预期结果是:

0 =>
id => 2
id_requestor => 2
jam_input => 2015-07-04 06:23:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 15

1 =>
id => 3
id_requestor => 3
jam_input => 2015-07-04 07:24:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 15

2 =>
id => 4
id_requestor => 4
jam_input => 2015-07-04 05:09:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 10

3 =>
id => 1
id_requestor => 1
jam_input => 2015-07-04 08:00:00
jam_pakai => 2015-07-04 08:30:00
total_poin => 10

请帮助^^ 非常感谢你。

1 个答案:

答案 0 :(得分:0)

按用户排序。如果total_poins相等,则使用strtotime比较时间

usort($arrayParam, function($a, $b) { 
        return  ($ret =  $b['total_poin'] - $a['total_poin'] ) ? 
                 $ret : 
                 strtotime($a['jam_input']) - strtotime($b['jam_input']); } );
print_r($arrayParam);