trifek's avatar

Sort table

Hi, I have problem with sort my large array.

dd($myarray[0]);

return me:

array:6 [▼ // app/Services/PresenceService.php:190
  "week1" => array:7 [▼
    0 => array:7 [▼
      "isEditable" => false
      "input" => "<input class="form-check-input" type="checkbox" name ="value-470" value = "1" >"
      "checked" => 0
      "isDirectorDay" => 0
      "date" => "2024-04-01"
      "lastAcceptedDay" => "2024-04-08"
      "present" => 0
    ]
    1 => array:7 [▼
      "isEditable" => false
      "input" => "<input class="form-check-input" type="checkbox" disabled checked>"
      "checked" => 1
      "isDirectorDay" => 1
      "date" => "2024-04-02"
      "lastAcceptedDay" => "2024-04-08"
      "present" => 1
    ]
    2 => array:7 [▼
      "isEditable" => false
      "input" => "<input class="form-check-input" type="checkbox" disabled checked>"
      "checked" => 1
      "isDirectorDay" => 1
      "date" => "2024-04-03"
      "lastAcceptedDay" => "2024-04-08"
      "present" => 1
    ]
    3 => array:7 [▼
      "isEditable" => false
      "input" => "<input class="form-check-input" type="checkbox" name ="value-473" value = "1" >"
      "checked" => 0
      "isDirectorDay" => 0
      "date" => "2024-04-04"
      "lastAcceptedDay" => "2024-04-08"
      "present" => 0
    ]
    4 => array:7 [▶]
    5 => array:7 [▶]
    6 => array:7 [▶]
  ]
  "week2" => array:7 [▶]
  "week3" => array:7 [▶]
  "week4" => array:7 [▶]
  "week5" => array:7 [▶]
  "info" => array:4 [▼
    "name" => "Adamowicz2 Paweł1"
    "totalDays" => 35
    "presentDays" => 5
    "frequency" => 14.29
  ]
]

In my array I have about 100 records.

I need sort my array by info=>name.

How can I make it?

Please help me

0 likes
3 replies
Tray2's avatar

First you need to know where the data comes from, if it comes from the database, you sort it there first,

Snapey's avatar

presume you want all members of myArray sorted by name? A pattern I often use is to hoist the data you want to the top and make it the array key

$mapped = Arr::mapWithKeys($myarray, function (array $item, int $key) {
    return [$item['info']['name'] => $item];
});

then you can sort the array by the key, eg

@foreach(ksort($mapped) as $item)

Please or to participate in this conversation.