Advertisement
krot

usort сортировка по совпадению

Jan 28th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. usort($dbDetails,  function  ($a, $b) use ($detail) {
  2.     $adate = (strpos($a['detail_num'], $detail) !== false);
  3.     $bdate = (strpos($b['detail_num'], $detail) !== false);
  4.     if (!($adate ^ $bdate)) return strcmp($a, $b);
  5.     return $adate ? -1 : 1;
  6.     });
  7.  
  8.         if(!empty($dbDetails)){
  9.             $result=array();
  10.             foreach($dbDetails as $key=>$val){
  11.                 if((strcasecmp($detail,$val['detail_num'])==0)){
  12.                     unset($val['detail_analog']);
  13.                     $val['detail_parent']=1;       
  14.                 array_unshift($result,$val);
  15.                 }
  16.                 else array_push($result,$val);
  17.                 unset($dbDetails[$key]);
  18.                
  19.             }
  20.             unset($dbDetails);
  21.             $dbDetails =$result;
  22.            
  23.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement