0

Base material :

I have a base list $food :

[{"id":"123","name":"apple"},{"id":"456","name":"Rost Beef"},...]

that i first reduce with $food_reduced = array_count_values(array_columns($food, "id"));.

[{123:2},{456:5},...]

Then i loop through $food_reduced with foreach($food_reduced as $id => $count) to query a DataBase to get the category (vegetables, fruits, meat, fish, etc..) that i store into $food_byCategory = array(); declared right before the loop :

 if ($food_byCategory[$row["ctg_name"]])
 {
      $food_byCategory[$row["ctg_name"]] += $count;
 }
 else
 {
      $food_byCategory[$row["ctg_name"]] = $count;
 }

Which gives me this array that I want to sort (gettype() says it's an Array at least) in PHP :

{
        Vegetable: 2
        Fruit: 1
        Fish: 5
        Drinks: 1
        Meat: 2
        Desert: 3
}

to this :

{
        Fish: 5
        Desert: 3
        Meat: 2
        Vegetable: 2
        Drinks: 1
        Fruit: 1
}

Notice the sorting by key once the sorting by value is done.

The goal is then to display this list into an html list by value descending.

I have tried asort, sort, arsort, with or without the option SORT_NUMERIC etc...
Wether it's not working at all or it's erasing the keys and replacing them with indexes.

I've also tried typecasting it into an array, but nothing changed (probably because it is an Array already ? But yet, not an Object ?)

I'm kinda lost. Help ?


SOLVED my issue by building the "count" array another way :

 if (!in_array($ctg_id, array_column($food_byCategory, "id")))
 {
      $food_byCategory[] = array(
           "id" => $ctg_id,
           "count" => 1
      );
 }
 else
 {
      $index = array_search($ctg_id, array_column($food_byCategory, "id"));
      $food_byCategory[$index]["count"]++;
 }

and then sorting it with usort :

usort($food_byCategory, function($a, $b) {
    return $b["count"] <=> $a["count"];
});
Bo. Ga.
  • 138
  • 1
  • 4
  • 12

2 Answers2

0

asort() applies to associative arrays, not objects.

You can easily transform your object into a associative array by typecasting it as describe here.

$array = (array) $yourObject;
E-telier
  • 766
  • 5
  • 15
  • Did not work, but probably du to the fact that it was not an object i guess... :x – Bo. Ga. Dec 03 '21 at 20:33
  • What is it then? A JSON? If so you could use json_decode($json, true); to convert it to an associative array. – E-telier Dec 03 '21 at 20:40
  • I have edited original question to explained how i built it. But if it is an array indeed, why sort() functions are not working ? My literal strings are erased when it's working., but i want to keep them. – Bo. Ga. Dec 03 '21 at 20:49
  • Are you sure you are using asort() on an associative array? Because it should do exactly what you are looking for... Here is the PHP manual : https://www.php.net/manual/en/function.asort.php – E-telier Dec 04 '21 at 15:16
-1

You can use a php function called sort, try something like this:

$myArray = array(4, 6, 2, 22, 11);
sort($myArray);

result: 2, 4, 6, 11, 22

or

$myArray = array(4, 6, 2, 22, 11);
rsort($myArray);

result: 22, 11, 6, 4, 2