Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: M. Chris
Added: Mar 14, 2018 10:27 AM
Views: 18
Tags: no tags
  1. /**
  2.      * Sort results array given list of values (field parameter).
  3.      *
  4.      * @param $field
  5.      * @param array $array
  6.      * @param array $list
  7.      *
  8.      * @return array
  9.      */
  10.     public function sortResultsByUserDefinedList($field, array $array, array $list)
  11.     {
  12.         if (count($list) > count($array)) {
  13.             throw new \RuntimeException('List size cannot be higher than inputted array.');
  14.         }
  15.  
  16.         $sortedArray = $list;
  17.         foreach ($array as $key => $value) {
  18.             if (false !== $sortIndex = array_search($array[$key][$field], $list)) {
  19.                 $sortedArray[$sortIndex] = $value;
  20.             }
  21.         }
  22.  
  23.         return $sortedArray;
  24.     }