<?php

namespace App\Utils;

class Utils
{
    /**
     * @param array $array
     * @param string $colName
     * @return array
     */
    public static  function sortArray(array $array, string $colName) : array {
        /* sort by date   **/
        /* https://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date/2910642 */
        usort($array, function ($a, $b) use(&$colName){
            return $a[$colName] - $b[$colName];});

        return $array;

    }

    /**
     * Function that groups an array of associative arrays by some key.
     *
     * @param {String} $key Property to sort by.
     * @param {Array} $data Array that stores multiple associative arrays.
     */
    public static function group_by($key, $data): array
    {
        $result = array();

        foreach($data as $val) {
            if(array_key_exists($key, $val)){
                $result[$val[$key]][] = $val;
            }else{
                $result[""][] = $val;
            }
        }

        return $result;
    }
}