Remove duplicates from array based on a specific key

<?php
/**
 * @param $array, represent your array
 * @param $key, represent your array key that need to be unique. e.g. ID
 * @return array, it will return a final array that will contain unique items
 */
function key_filter($array, $key)
{
    $keys_list = [];
    $array_final = [];
    foreach ($array as $item):
        if (!in_array($item[$key], $keys_list)):
            $keys_list[] = $item[$key];
            $array_final[] = $item;
        endif;
    endforeach;
    return $array_final;
}

 

Top