0

I'm tracking how many words appear in the description of my products and created a multidimensional array that is keeping track but I need to sort them by the most found keywords to the lowest. I have this array and I want to be able to sort it by the "count" key:

Array
(
[KOM1-K182924DA] => Array
    (
        [count] => 1
        [words] => Array
            (
                [4] => cotton
            )

    )

[PVH1-U2666001] => Array
    (
        [count] => 2
        [words] => Array
            (
                [2] => cotton
                [5] => briefs
            )

    )

[GEO2-K2345TF] => Array
    (
        [count] => 1
        [words] => Array
            (
                [4] => red
            )

    )

[KOM1-K182871HK] => Array
    (
        [count] => 3
        [words] => Array
            (
                [4] => cotton
                [5] => nylon
                [6] => blue
            )

    )
)

So the result would look like this:

Array
(
[KOM1-K182871HK] => Array
    (
        [count] => 3
        [words] => Array
            (
                [4] => cotton
                [5] => nylon
                [6] => blue
            )

    )

[PVH1-U2666001] => Array
    (
        [count] => 2
        [words] => Array
            (
                [2] => cotton
                [5] => briefs
            )

    )
[KOM1-K182924DA] => Array
    (
        [count] => 1
        [words] => Array
            (
                [4] => cotton
            )

    )



[GEO2-K2345TF] => Array
    (
        [count] => 1
        [words] => Array
            (
                [4] => red
            )

    )
)

How can I do this? I've tried multiple solutions that I found on stackoverflow but none of them have worked for me. I've tried the solution on here Sort Multi-dimensional Array by Value. I'm using PHP 5.6 so I tried this:

usort($myArray, function($a, $b) {
return $a['order'] - $b['order'];
});

But it's not returning an array, instead it returns the "sku" and a digit.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
MalcolmInTheCenter
  • 1,376
  • 6
  • 27
  • 47

2 Answers2

0

Extract the count column into a single dimension, sort that descending and sort the original array by that:

array_multisort(array_column($myArray, 'count'), SORT_DESC, $myArray);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
-2

you may use array_multisort or usort

working solution:

function sortBySubValue($array, $value, $asc = true, $preserveKeys = false)
{
    if ($preserveKeys) {
        $c = array();
        if (is_object(reset($array))) {
            foreach ($array as $k => $v) {
                $b[$k] = strtolower($v->$value);
            }
        } else {
            foreach ($array as $k => $v) {
                $b[$k] = strtolower($v[$value]);
            }
        }
        $asc ? asort($b) : arsort($b);
        foreach ($b as $k => $v) {
            $c[$k] = $array[$k];
        }
        $array = $c;
    } else {
        if (is_object(reset($array))) {
            usort($array, function ($a, $b) use ($value, $asc) {
                return $a->{$value} == $b->{$value} ? 0 : ($a->{$value} - $b->{$value}) * ($asc ? 1 : -1);
            });
        } else {
            usort($array, function ($a, $b) use ($value, $asc) {
                return $a[$value] == $b[$value] ? 0 : ($a[$value] - $b[$value]) * ($asc ? 1 : -1);
            });
        }
    }

    return $array;
}

sortBySubValue($array, 'count', false, true);
Swatantra Kumar
  • 1,324
  • 5
  • 24
  • 32
  • This should be a comment as you literally have no description on how to do it, it's not really a valid answer. Also this question is a duplicate. – GrumpyCrouton Aug 08 '17 at 16:59
  • Example 2 on the usort page Kumar linked is close enough to this question that it could answer it. – jh1711 Aug 08 '17 at 17:04
  • @jh1711 It's not the issue. The issue is that 1. simple links answers are invalid (automatically detected and appearing in admin questions review queue) and 2. a question asked and answered many times already on the website should be closed, not answered another time. – Sebas Aug 08 '17 at 17:05
  • @Sebas, I did not mean to disagree with GrumpyCroutons comment. I just wanted to point altoids to the right portion of the manual. But I should have worded it differently, – jh1711 Aug 08 '17 at 17:22