0

I want to sort a nested array by number. So that in each category the products with the biggest number comes first.

I tried something i found here. How to Sort Multi-dimensional Array by Value?

usort($myArray, function($a, $b) {
    $retval = $a['order'] <=> $b['order'];
    if ($retval == 0) {
        $retval = $a['suborder'] <=> $b['suborder'];
        if ($retval == 0) {
            $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
        }
    }
    return $retval;
});

but found no solution to make it work. Especially because i don't know each productnumber (e.g.sdk38z3) inside one category (e.g. chocholate - sugar - candy). In my case i used uasort instead of usort.

Array
(
    [apple - oranges - bananas] => Array
        (
            [fn7z3] => Array
                (
                    [slider_details] =>
                    [productnumber] => fn7z3
                    [category] => 
                    [title] => 
                    [text] => 
                    [picture] => 
                    [number] => 30
                    [oneString] =>
                )

        )

    [chocholate - sugar - candy] => Array
        (
            [sdk38z3] => Array
                (
                    [slider_details] =>
                    [productnumber] => sdk38z3
                    [category] => 
                    [title] => 
                    [text] => 
                    [picture] => 
                    [number] => 45
                    [oneString] => 
                )

            [g433] => Array
                (
                    [slider_details] =>
                    [productnumber] => g433
                    [category] => 
                    [title] => 
                    [text] => 
                    [picture] => 
                    [number] => 2
                    [oneString] =>
                )

            [j8z28] => Array
                (
                    [slider_details] =>
                    [productnumber] => j8z28
                    [category] => 
                    [title] => 
                    [text] => 
                    [picture] => 
                    [number] => 250
                    [oneString] =>
                )

            [73hf873] => Array
                (
                    [slider_details] => 
                    [productnumber] => 73hf873
                    [category] => 
                    [title] => 
                    [text] => 
                    [picture] =>
                    [number] => 30
                    [oneString] => 
                )

        )

Result i want:

Array
(
    [apple - oranges - bananas] => Array
        (
            [fn7z3] => Array
                (
                    [slider_details] =>
                    [productnumber] => fn7z3
                    [category] => 
                    [title] => 
                    [text] => 
                    [picture] => 
                    [number] => 30
                    [oneString] =>
                )

        )

    [chocholate - sugar - candy] => Array
        (
            [j8z28] => Array
                (
                    [slider_details] =>
                    [productnumber] => j8z28
                    [category] => 
                    [title] => 
                    [text] => 
                    [picture] => 
                    [number] => 250
                    [oneString] =>
                )
            [sdk38z3] => Array
                (
                    [slider_details] =>
                    [productnumber] => sdk38z3
                    [category] => 
                    [title] => 
                    [text] => 
                    [picture] => 
                    [number] => 45
                    [oneString] => 
                )

            [73hf873] => Array
                (
                    [slider_details] => 
                    [productnumber] => 73hf873
                    [category] => 
                    [title] => 
                    [text] => 
                    [picture] =>
                    [number] => 30
                    [oneString] => 
                )

            [g433] => Array
                (
                    [slider_details] =>
                    [productnumber] => g433
                    [category] => 
                    [title] => 
                    [text] => 
                    [picture] => 
                    [number] => 2
                    [oneString] =>
                )
        )
iso
  • 1
  • 1

1 Answers1

0

I finally found a solution for my problem, by using a function given in this answer: How to sort an array of associative arrays by value of a given key in PHP?

Because i don't really know the key i need i used a foreach loop with the $key functionality.

I needed to override my actual array with the sorted array too, to make changes permanent. In my case, as i said, im using uasort instead of usort, because im using names as Key.

Explanation of variables:

$similarProducts = my whole array.

$attributeCombination = for Example [apple - oranges - bananas]

function invenDescSort($item1, $item2)
{
    if ($item1['number'] == $item2['number']) return 0;
    return ($item1['number'] < $item2['number']) ? 1 : -1;
}

foreach ($similarProducts AS $key => $attributeCombination) {

    uasort($attributeCombination, 'invenDescSort');
    $this->similarProducts[$key] = $attributeCombination;
    //print_r($attributeCombination);

}
iso
  • 1
  • 1