Как сортировать по двум значениям, а затем поле ASC в массиве с использованием usort () в PHP?

Я проанализировал три разных текстовых файла:

space.txt

Kournikova Anna FF 6-3-1975 Red Hingis Martina MF 4-2-1979 Green Seles Monica HF 12-2-197 

comma.txt

 Abercrombie, Neil, Male, Tan, 2/13/1943 Bishop, Timothy, Male, Yellow, 4/23/1967 Kelly, Sue, Female, Pink, 7/12/1959 

pipe.txt

 Smith | Steve | D | M | Red | 3-3-1985 Bonk | Radek | S | M | Green | 6-3-1975 Bouillon | Francis | G | M | Blue | 6-3-1975 

Я использовал следующий код для анализа всех файлов в один массив ………….

 <?php $space_txt = './data/input/space.txt'; $comma_txt = './data/input/comma.txt'; $pipe_txt = './data/input/pipe.txt'; $parsed_space_data = file_get_contents($space_txt); $parsed_comma_data = file_get_contents($comma_txt); $parsed_pipe_data = file_get_contents($pipe_txt); $space_array = myExpldeLoopFunc("space"," ", $parsed_space_data); $comma_array = myExpldeLoopFunc("comma",",", $parsed_comma_data); $pipe_array = myExpldeLoopFunc("pipe"," | ", $parsed_pipe_data); $finalArray = array_merge($space_array, $comma_array, $pipe_array); function changeGender($gender) { if($gender === 'F') { return str_replace('F', 'Female', $gender); } elseif($gender === 'M') { return str_replace('M', 'Male', $gender); } } function normalizeDate($date) { return str_replace('-', '/', $date); } function myExpldeLoopFunc($name, $sep, $data) { $parsedData = explode("\r", $data); $arr = []; foreach ($parsedData as $data) { $data_arr = explode($sep, $data); if($name == 'space'){ $arr[] = [ "last_name" => $data_arr[0], "first_name" => $data_arr[1], // "middle_initial" => $data_arr[2], "gender" => changeGender($data_arr[3]), "date_of_birth" => normalizeDate($data_arr[4]), "favorite_color" => $data_arr[5] ]; } elseif($name == 'comma') { $arr[] = [ "last_name" => $data_arr[0], "first_name" => $data_arr[1], "gender" => $data_arr[2], "date_of_birth" => normalizeDate($data_arr[4]), "favorite_color" => $data_arr[3] ]; } elseif ($name == 'pipe') { $arr[] = [ "last_name" => $data_arr[0], "first_name" => $data_arr[1], // "middle_initial" => $data_arr[2], "gender" => changeGender($data_arr[3]), "date_of_birth" => normalizeDate($data_arr[5]), "favorite_color" => $data_arr[4] ]; } } return $arr; } for ($i=0; $i < count($finalArray); $i++) { foreach ($finalArray as $key => $row) { $gender[$key] = $row['gender']; $last_name[$key] = $row['last_name']; } array_multisort($gender, SORT_ASC, $last_name, SORT_ASC, $finalArray); echo join(' ', $finalArray[$i]) . '<br>'; } var_dump($finalArray); ?> 

Теперь у меня есть следующий массив ………..

 array (size=9) 0 => array (size=5) 'last_name' => string 'Kournikova' (length=10) 'first_name' => string 'Anna' (length=4) 'gender' => string 'Female' (length=6) 'date_of_birth' => string '6/3/1975' (length=8) 'favorite_color' => string 'Red' (length=3) 1 => array (size=5) 'last_name' => string ' Hingis' (length=7) 'first_name' => string 'Martina' (length=7) 'gender' => string 'Female' (length=6) 'date_of_birth' => string '4/2/1979' (length=8) 'favorite_color' => string 'Green' (length=5) 2 => array (size=5) 'last_name' => string ' Seles' (length=6) 'first_name' => string 'Monica' (length=6) 'gender' => string 'Female' (length=6) 'date_of_birth' => string '12/2/1973' (length=9) 'favorite_color' => string 'Black' (length=5) 3 => array (size=5) 'last_name' => string 'Abercrombie' (length=11) 'first_name' => string ' Neil' (length=5) 'gender' => string ' Male' (length=5) 'date_of_birth' => string ' 2/13/1943' (length=10) 'favorite_color' => string ' Tan' (length=4) 4 => array (size=5) 'last_name' => string ' Bishop' (length=7) 'first_name' => string ' Timothy' (length=8) 'gender' => string ' Male' (length=5) 'date_of_birth' => string ' 4/23/1967' (length=10) 'favorite_color' => string ' Yellow' (length=7) 5 => array (size=5) 'last_name' => string ' Kelly' (length=6) 'first_name' => string ' Sue' (length=4) 'gender' => string ' Female' (length=7) 'date_of_birth' => string ' 7/12/1959' (length=10) 'favorite_color' => string ' Pink' (length=5) 6 => array (size=5) 'last_name' => string 'Smith' (length=5) 'first_name' => string 'Steve' (length=5) 'gender' => string 'Male' (length=4) 'date_of_birth' => string '3/3/1985' (length=8) 'favorite_color' => string 'Red' (length=3) 7 => array (size=5) 'last_name' => string ' Bonk' (length=5) 'first_name' => string 'Radek' (length=5) 'gender' => string 'Male' (length=4) 'date_of_birth' => string '6/3/1975' (length=8) 'favorite_color' => string 'Green' (length=5) 8 => array (size=5) 'last_name' => string ' Bouillon' (length=9) 'first_name' => string 'Francis' (length=7) 'gender' => string 'Male' (length=4) 'date_of_birth' => string '6/3/1975' (length=8) 'favorite_color' => string ' Blue' (length=4) 

До сих пор выход ……..

 Kelly Sue Female 7/12/1959 Pink Bishop Timothy Male 4/23/1967 Yellow Abercrombie Neil Male 2/13/1943 Tan Hingis Martina Female 4/2/1979 Green Seles Monica Female 12/2/1973 Black Kournikova Anna Female 6/3/1975 Red Bonk Radek Male 6/3/1975 Green Bouillon Francis Male 6/3/1975 Blue Smith Steve Male 3/3/1985 Red 

Я хочу отсортировать массив по Females, затем Males, затем по last_name asc ……..

 Hingis Martina Female 4/2/1979 Green Kelly Sue Female 7/12/1959 Pink Kournikova Anna Female 6/3/1975 Red Seles Monica Female 12/2/1973 Black Abercrombie Neil Male 2/13/1943 Tan Bishop Timothy Male 4/23/1967 Yellow Bonk Radek Male 6/3/1975 Green Bouillon Francis Male 6/3/1975 Blue Smith Steve Male 3/3/1985 Red 

Я тоже пробовал ……

 function sortBy($field, &$array, $direction = 'asc') { usort($array, create_function(' $a, $b', ' $a = $a["' . $field . '"]; $b = $b["' . $field . '"]; if ($a == $b) { return 0; } return ($a ' . ($direction == 'desc' ? '>' : '<') .' $b) ? -1 : 1; ')); return true; } for ($i=0; $i < count($finalArray); $i++) { sortBy('gender', $finalArray); sortBy('last_name', $finalArray); echo join(' ', $finalArray[$i]) . '<br>'; } 

Я попробовал array_multisort (), usort (), sort (), asort (), и я все еще не мог получить результаты, которые я хотел. Какое решение может быть использовано для получения результата?

Related of "Как сортировать по двум значениям, а затем поле ASC в массиве с использованием usort () в PHP?"