dimanche 28 juin 2015

Add value in specific index array

i have array data like this.

[0] => Array (
    [id] => 1
    [id_requestor] => 1
    [jam_input] => 2015-06-20 06:00:00
    [jam_pakai] => 2015-06-28 08:00:00
    [total_poin] => 30
    )
[1] => Array (
    [id] => 2
    [id_requestor] => 2
    [jam_input] => 2015-06-20 07:00:00
    [jam_pakai] => 2015-06-28 08:00:00
    [total_poin] => 10
    )
[2] => Array (
    [id] => 3
    [id_requestor] => 3
    [jam_input] => 2015-06-20 06:30:00
    [jam_pakai] => 2015-06-28 08:00:00
    [total_poin] => 5
    )

In above data, there is total_poin. This total_poin that i use later. I want to sort total_poin array descending. But i wont to use php built in array function. Cause i have to use method from research paper like this.

for i=0 to i<main queue.size
   if jobi+1 length > jobi length then
    add jobi+1 in front of job i in the queue
end if
   if main queue.size = 0 then
    add job i last in the main queue
end if

And here is my implementation :

function LJFAlgorithm($time_str) {
        $batchData = getBatch($time_str);

        $ljf = [];
        for ($i=0; $i < count($batchData)-1; $i++) { 
            echo $batchData[$i+1]['total_poin'] ." >= ". $batchData[$i]['total_poin'] . " = " . ($batchData[$i+1]['total_poin'] >= $batchData[$i]['total_poin']);

            if ($batchData[$i+1]['total_poin'] >= $batchData[$i]['total_poin']) {
                echo ", Add " . $batchData[$i+1]['total_poin'] . " in front of " . $batchData[$i]['total_poin'];
            } else {
                echo ", Add " . $batchData[$i]['total_poin'] . " in front of " . $batchData[$i+1]['total_poin'];
            }

            echo '<br/>';
        }

        print_r($ljf);
    }

But it not work perfectly, i get one data missing. Here is my output code :

10 >= 30 = , Add 30 in front of 10
5 >= 10 = , Add 10 in front of 5

5 value not added in array. How to fix that?

Thank you very much.

Aucun commentaire:

Enregistrer un commentaire