-1

I tried implementing a solution for my 3-level multi-dimensional array. The problem arose when I realised I can't use keynames to target a value as they're dynamically created (SKU-style).

A skimmed version of my array data:

Array
(
    ['weeks'] => Array
    (
        [14] => Array
            (
                [some_sku] => 28.5
                [some_sku_1] => 10.8
                [some_sku_2] => 11.21
                [some_sku_3] => 19.98
                [some_sku_4] => 20.79
            )
        [31] => Array
            (
                [some_sku_1] => 28.5
                [some_sku_5] => 10.8
                [some_sku_6] => 11.21
                [some_sku_7] => 19.98
                [some_sku_9] => 20.79
            )
    )
)

I thought maybe I could sort the individual arrays within the "mothership" array but that doesn't seem to achieve what I want:

foreach ($data as $item)
{
    foreach ($item as $el)
    {
        usort($el, function($a, $b){return $a > $b;});
    }
}

But the data remains the same: https://3v4l.org/E6W8Z

My guess is that whilst it may happen it doesn't store the values in the array. I also can't assign the output of usort() as that returns a boolean instead of the sorted array.

How am I able to sort my child arrays within the parent array by value (high - low)?

0

1 Answer 1

0

You can try like this (note uasort instead of usort if you want to keep your index "some_sku_"):

foreach($data as $key1 => $value1)
{
    foreach($value1 as $key2 => $value2)
    {
        uasort($value2, function($a, $b)
        {
            return strcasecmp($a, $b);
        });
        $value1[$key2] = $value2;
    }
    $data[$key1] = $value1;
}

Simplify into this (note the "&" who permit to assigning that array by reference, who permit to work on the original variable directly, also without the "&" the variable is lost, so in this case you need the first solution with "$data[$key]=$val") :

  foreach($data as &$child1)
    {
        foreach($child1 as &$child2)
        {
            uasort($child2, function($a, $b)
            {
                return strcasecmp($a, $b);
            });
        }
    }

Results :

 Array
    (
        [weeks] => Array
            (
                [14] => Array
                    (
                        [some_sku_1] => 10.8
                        [some_sku_2] => 11.21
                        [some_sku_3] => 19.98
                        [some_sku_4] => 20.79
                        [some_sku] => 28.5
                    )

                [31] => Array
                    (
                        [some_sku_5] => 10.8
                        [some_sku_6] => 11.21
                        [some_sku_7] => 19.98
                        [some_sku_9] => 20.79
                        [some_sku_1] => 28.5
                    )
            )
    )
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, I actually found a dupe for this that works well - will be closed or removed at some point (sorry!)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.