Thursday, 30 August 2018

Add to the table, but keep the indexes with PHP

I have an array that are already sorted.

Now I would like to take all the arrays that have a sub array value 0 and put them at the start of the array.
This is what I tried to do:
        foreach($dealStatsArray as $deal_id => $dealStats)
        {
            if($dealStats['revenueY'] == 0)
            {

                $tmpArray[$deal_id] = $dealStats; // Store the array
                unset($dealStatsArray[$deal_id]); // Unset the current one, since it is not in right position

                array_unshift($dealStatsArray, $tmpArray); // Prepend the tmp array, to have it at the beginning of the array
            }
        }

Now problem is that array_unshift() does:
"All numerical array keys will be modified to start counting from zero" -php net array_unshift()
Which mess up the rest of the code I got because I need to keep the indexes on $dealStatsArray, and the index for the new prepended array should be the $deal_id and not 0.
How can I do this? And I need a solution that can manage to prepend 2 or 3 times to the beginning of the array, just like it works fine with array_push (appending) I would like to do that, but just prepending
Update: Here is my currently uasort function, that are sorting the array after the revenueY value, so that the highest number are in the start of the array and then descending..
function cmp($a, $b)
{
    if (($a["revenueY"]) == ($b["revenueY"])) {
        return 0;
    }
    return (($a["revenueY"]) > ($b["revenueY"])) ? -1 : 1;
}

uasort($dealStatsArray, "cmp");

Now if I follow @thaJeztah's answer, which partly works, then I added this under:
function sortbyRevenueY($a, $b) {
    if ($a['revenueY'] == $b['revenueY']) {
       return 0;
    }
    return ($a['revenueY'] == 0) ? -1 : 1;
}

uasort($dealStatsArray, 'sortbyRevenueY');

But this does not work correct, It does take all the revenueY==0 arrays and prepend at the beginning of the array, but then the rest of the arrays gets unsorted (highest to lowest, the first uasort())
Here's my final goal: To have an array where all the revenueY==0 are at the beginning of the array, and after these the top revenue are coming after and then descending to lowest revenue at the end of the array.

You can probably achieve this by 'sorting' the array using a custom callback and uasort();
function sortbyRevenueY($a, $b) {
    if ($a['revenueY'] == $b['revenueY']) {
       return 0;
    }

    if (0 == $a['revenueY']) {
       return -1;
    }

    if (0 == $b['revenueY']) {
       return 1;
    }

    return (($a["revenueY"]) > ($b["revenueY"])) ? -1 : 1;
}

uasort($dealStatsArray, 'sortbyRevenueY');

print_r($dealStatsArray);

Haven't been able to test it and the 'callback' method may need some tweaking, however, uasort() allows you to sort an array using a custom 'callback' method that determines 'how' the array should be sorted. The values inside the array will be sorted, without losing the key/value relation.
My example is just to illustrate how you can achieve this, but, as mentioned, may need to be tweaked.
[update] I've updated by example to attempt to combine 'regular' sorting and sorting by '0' into a single callback. Again untested, but maybe this work. Please test this updated example.

0 comments:

Post a Comment