Hallo,

Ik heb mijn eigen array opgebouwd met array_push en wil daarna alles sorteren op prijs en dan printen.
Er zijn veel methodes te vinden op internet maar krijg het niet werkende in de praktijk ( array_multisort(), asort(), sort() )

Zijn er experts aanwezig die hier ervaring mee hebben en mij kunnen helpen ?

Alvast bedankt, nog een fijn weekend!


Array
(
    [0] => Array
        (
            [price] => $0.25
            [name] => Operation Phoenix Weapon Case
            [condition] => n/a
        )

    [1] => Array
        (
            [price] => $0.12
            [name] => Huntsman Weapon Case
            [condition] => n/a
        )

    [2] => Array
        (
            [price] => $25.71
            [name] => AK-47 | Redline
            [condition] => Minimal Wear
        )

    [3] => Array
        (
            [price] => $24.54
            [name] => StatTrak Desert Eagle | Conspiracy
            [condition] => Factory New
        )
)
Het is eigenlijk vrij simpel, maar de namen zijn vrij moeilijk:

[php]sort[/php] - De basis sort functie, deze sorteert op value van laagste tot hoogste (0 - 9, a - z, etc.)
[php]rsort[/php] - De basis sort, maar dan andersom (de "r" prefix komt van "reverse")
[php]ksort[/php] - Zelfde als basis sort, maar dan met keys ipv values (vandaar de "k" prefix)
[php]krsort[/php] - Combinatie van rsort en ksort
[php]asort[/php] - Basis sort functie, maar dan blijven de array keys intact (dus array([1] => 3, [2] => 2) wordt array([2] => 2, [1] => 3)). De "a" prefix komt van assoctiative.
[php]arsort[/php] - Combinatie van asort en rsort
[php]usort[/php] - Een sort functie die een functie gebruikt om te sorteren (de "u" van "user function")
[php]uasort[/php] - Combinatie van asort en usort
[php]uksort[/php] - Combinatie van ksort en usort

In jouw geval wil je een user defined sort functie definiƫren, om op die manier de prijzen te vergelijken:
<?php

$products = array(...);

usort($products, function ($productA, $productB) {
if ($productA['price'] === $productB['price']) {
return 0; // prijzen zijn gelijk, laat de elementen zo staan
}

if ($productA['price'] > $productB['price']) {
return 1; // prijs A is groter dan prijs B, verplaats A dus na B
}

return -1; // prijs A is kleiner dan prijs B, verplaats A dus voor B
});
?>
Sorteren op price gaat zo niet lukken.
Price is een string en geen getal.
Dus $8.00 is groter dan $10.00
Ik heb met str_replace('&#36;', '', $val); de '$' weg kunnen halen.
https://dl.dropboxusercontent.com/u/358029804/prices.json
Maar na het gebruiken van de functie lukt het nog steeds niet, hier de code :
<?php
function cmp($a, $b)
{
return $a['price'] - $b['price'];
}

usort($array, 'cmp');

echo '<pre>';
print_r( $array );
exit;
?>
Voor :

Array
(
    [0] => Array
        (
            [price] => 0.10
            [name] => SSG 08 | Slashed
            [condition] => Field-Tested
        )

    [1] => Array
        (
            [price] => 0.04
            [name] => Sticker Capsule 2
            [condition] => n/a
        )

    [2] => Array
        (
            [price] => 0.03
            [name] => Sticker Capsule
            [condition] => n/a
        )

    [3] => Array
        (
            [price] => 0.63
            [name] => Winter Offensive Weapon Case
            [condition] => n/a
        )

    [4] => Array
        (
            [price] => 0.17
            [name] => Operation Breakout Weapon Case
            [condition] => n/a
        )

    [5] => Array
        (
            [price] => 0.04
            [name] => Nova | Sand Dune
            [condition] => Field-Tested
        )
)

Na :

Array
(
    [0] => Array
        (
            [price] => 0.04
            [name] => Tec-9 | Army Mesh
            [condition] => Minimal Wear
        )

    [1] => Array
        (
            [price] => 1.15
            [name] => Souvenir P250 | Bone Mask
            [condition] => Minimal Wear
        )

    [2] => Array
        (
            [price] => 0.78
            [name] => StatTrak Galil AR | Shattered
            [condition] => Well-Worn
        )

    [3] => Array
        (
            [price] => 0.18
            [name] => StatTrak MP7 | Urban Hazard
            [condition] => Field-Tested
        )

    [4] => Array
        (
            [price] => 0.11
            [name] => P90 | Leather
            [condition] => Field-Tested
        )

    [5] => Array
        (
            [price] => 0.26
            [name] => P250 | Steel Disruption
            [condition] => Factory New
        )
)

Er wel gebeurd degelijk iets, maar price is nog steeds niet correct gesorteerd.
Iemand enig idee wat er fout gaat? Alvast bedankt voor jullie tijd!
Niet - (min)
return $a['price'] - $b['price'];

Maar > (groter dan)
return $a['price'] > $b['price'];
Dat werkt! Dankjewel!

Reageren