0

I have the following array:

Array
(
    [0] => Array
        (
            [last_name] => Smith
            [first_name] => Steve
            [gender] => Male
            [date_of_birth] => 3/3/1985
            [favorite_color] => Red
        )

    [1] => Array
        (
            [last_name] => Seles
            [first_name] => Monica
            [gender] => Female
            [date_of_birth] => 12/2/1973
            [favorite_color] => Black
        )

    [2] => Array
        (
            [last_name] => Kournikova
            [first_name] => Anna
            [gender] => Female
            [date_of_birth] => 6/3/1975
            [favorite_color] => Red
        )

    [3] => Array
        (
            [last_name] => Kelly
            [first_name] => Sue
            [gender] => Female
            [date_of_birth] => 7/12/1959
            [favorite_color] => Pink
        )

    [4] => Array
        (
            [last_name] => Hingis
            [first_name] => Martina
            [gender] => Female
            [date_of_birth] => 4/2/1979
            [favorite_color] => Green
        )

    [5] => Array
        (
            [last_name] => Bouillon
            [first_name] => Francis
            [gender] => Male
            [date_of_birth] => 6/3/1975
            [favorite_color] => Blue
        )

    [6] => Array
        (
            [last_name] => Bonk
            [first_name] => Radek
            [gender] => Male
            [date_of_birth] => 6/3/1975
            [favorite_color] => Green
        )

    [7] => Array
        (
            [last_name] => Bishop
            [first_name] => Timothy
            [gender] => Male
            [date_of_birth] => 4/23/1967
            [favorite_color] => Yellow
        )

    [8] => Array
        (
            [last_name] => Abercrombie
            [first_name] => Neil
            [gender] => Male
            [date_of_birth] => 2/13/1943
            [favorite_color] => Tan
        )

)

Here is the code that did it..................

<?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) {


        return $gender === 'F' ? 'Female' : 'Male';
    }

    function normalizeDate($date) {
        return str_replace('-', '/', $date);
    }




    function myExpldeLoopFunc($name, $sep, $data) {

        $parsedData = explode("\n", $data);




        $arr = [];
        foreach ($parsedData as $data) {
            if ($data === "") continue;
            $data_arr = explode($sep, $data);





            if($name == 'space'){

                $arr[] = [
                    "last_name" => trim($data_arr[0]),
                    "first_name" => trim($data_arr[1]),
                    // "middle_initial" => trim($data_arr[2]),
                    "gender" => changeGender(trim($data_arr[3])),
                    "date_of_birth" => normalizeDate(trim($data_arr[4])),
                    "favorite_color" => trim($data_arr[5])



                ];








            }

                elseif($name == 'comma') {
                    $arr[] = [
                    "last_name" => trim($data_arr[0]),
                    "first_name" => trim($data_arr[1]),
                    "gender" => trim($data_arr[2]),
                    "date_of_birth" => normalizeDate(trim($data_arr[4])),
                    "favorite_color" => trim($data_arr[3])

                    ];
                }

            elseif ($name == 'pipe') {
                    $arr[] = [
                    "last_name" => trim($data_arr[0]),
                    "first_name" => trim($data_arr[1]),
                    // "middle_initial" => trim($data_arr[2]),
                    "gender" => changeGender(trim($data_arr[3])),
                    "date_of_birth" => normalizeDate(trim($data_arr[5])),
                    "favorite_color" => trim($data_arr[4])



                ];


            }





    }



    return $arr;







        }












 ?>

This is the code I use to sort the array by birth_date and last_name............

<?php 

 foreach ($finalArray as $key => $row) {
    $date_of_birth[$key] = $row['date_of_birth'];
    $last_name[$key] = $row['last_name'];
}

 array_multisort($date_of_birth, SORT_NUMERIC, $last_name, SORT_ASC, $finalArray);

 foreach ($finalArray as $row) {
    echo join(' ', $row) . "<br>\n";
 }  




  ?>

The following output is this .............................

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

The following output should look like this..............................

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

How can I sort the dates by 'year' in m/d/yyyy format along with the lastnames going in alphabetical order?

halfer
  • 18,701
  • 13
  • 79
  • 158
Michael Stokes
  • 381
  • 1
  • 8
  • 17
  • if you used a database this would be a billion times easier –  Jun 27 '16 at 01:17
  • It would be but in this case I couldn't. – Michael Stokes Jun 27 '16 at 01:18
  • [usort](http://php.net/manual/en/function.usort.php) will let you sort on whatever column(s) you need, just turn the dates either into [DateTime](http://php.net/manual/en/class.datetime.php) objects, or into unix timestamps with [strtotime](http://php.net/manual/en/function.strtotime.php). Both of which could be done in the callback. – Jeffrey Campbell Jun 27 '16 at 01:21
  • @john conde How its a duplicate? – Michael Stokes Jun 27 '16 at 01:38
  • I agree that it *isn't* duplicate. The problem for the OP is not, "how do I sort an array?", but, "how do I sort an array containing dates?" Jeffrey provides the essential answer: use a callback-function in `usort`, and, within this function, use PHP's rich library of date-handling functions. If the primary sort is "by name," that's what it tests first. If the names are equal, it tests dates. *All* of the monkey-business of "converting a string into a date," then extracting parts of dates or comparing dates, is provided by the language. – Mike Robinson Jun 27 '16 at 01:47
  • 2
    If you look at the duplicate you will see how to sort an array by *anything*. It can be dates or any other criteria. This question doesn't ask anything new that isn't covered in that question. – John Conde Jun 27 '16 at 01:49
  • Yeah but the dates has to be sorted by 'year'. Everything I do always results being sorted by the month. – Michael Stokes Jun 27 '16 at 01:50
  • Just resolved everything. Thanks anyways. – Michael Stokes Jun 27 '16 at 03:00

0 Answers0