Sort a multidimensional array with PHP

1

I have a multidimensional array and the code they gave me in response to another question does not work, it only works with a non-multidimensional array:

<?php
$array = Array(
            "Color"           => "YELLOW",
            "Variedad"        => "CHERRY",
            "Grado"           => "NORMAL",
            "Finca"           => "FINCA1",
            "Producto"        => "FLOWER1",
            "year"            => 2016,
            "week"            => 26,
            "quantity"        => 91570,
            "quantity_origin" => 2096
        );

print_r($array);

function my_sort($a,$b){
    $orden = array(   
                "Finca"           => 1, 
                "Producto"        => 2, 
                "Bloque"          => 3,
                "Variedad"        => 4, 
                "Color"           => 5, 
                "Grado"           => 6, 
                "year"            => 7, 
                "week"            => 8, 
                "quantity"        => 9, 
                "quantity_origin" => 10

            );

    return (($orden[$a])) < (($orden[$b])) ? -1 : 1;

}

uksort($array,"my_sort");

print_r($array);
?>

I do not want to order it alphabetically, but as it is in the mysort () function of the previous code, I tried it in the following way:

<?php
    $array = Array(
                  Array(
                    "Color"           => "YELLOW",
                    "Variedad"        => "CHERRY",
                    "Grado"           => "NORMAL",
                    "Finca"           => "FINCA1",
                    "Producto"        => "FLOWER1",
                    "year"            => 2016,
                    "week"            => 26,
                    "quantity"        => 91570,
                    "quantity_origin" => 2096
                  ),

                 Array(
                    "Color"           => "BLUE",
                    "Variedad"        => "CHERRY",
                    "Grado"           => "GOLD",
                    "Finca"           => "FINCA1",
                    "Producto"        => "FLOWER1",
                    "year"            => 2016,
                    "week"            => 26,
                    "quantity"        => 91570,
                    "quantity_origin" => 2096
                  ),
            );


    print_r($array);

    function my_sort($a,$b){
        $orden = array(
                   array(   
                    "Finca"           => 1, 
                    "Producto"        => 2, 
                    "Bloque"          => 3,
                    "Variedad"        => 4, 
                    "Color"           => 5, 
                    "Grado"           => 6, 
                    "year"            => 7, 
                    "week"            => 8, 
                    "quantity"        => 9, 
                    "quantity_origin" => 10
                   )
                );

        for($i=0; $i < count($orden); $i++){
           $orden = (($orden[$i][$a])) < (($orden[$i][$b])) ? -1 : 1;
        }     
        return $orden;

    }

    uksort($array,"my_sort");

    print_r($array);
    ?>

Note: Use Phalcon Framework for the code

    
asked by Fabian Sierra 14.07.2016 в 21:10
source

1 answer

1

What you have to go through with a loop is the multidimensional array, to apply the order to each internal array. The comparison function is a simple function to which 2 elements are passed and returns which is the largest according to the order.

 <?php
        $array = Array(
                      Array(
                        "Color"           => "YELLOW",
                        "Variedad"        => "CHERRY",
                        "Grado"           => "NORMAL",
                        "Finca"           => "FINCA1",
                        "Producto"        => "FLOWER1",
                        "year"            => 2016,
                        "week"            => 26,
                        "quantity"        => 91570,
                        "quantity_origin" => 2096
                      ),

                     Array(
                        "Color"           => "BLUE",
                        "Variedad"        => "CHERRY",
                        "Grado"           => "GOLD",
                        "Finca"           => "FINCA1",
                        "Producto"        => "FLOWER1",
                        "year"            => 2016,
                        "week"            => 26,
                        "quantity"        => 91570,
                        "quantity_origin" => 2096
                      ),
                );


        print_r($array);

        function my_sort($a,$b){
            $orden = array(   
                        "Finca"           => 1, 
                        "Producto"        => 2, 
                        "Bloque"          => 3,
                        "Variedad"        => 4, 
                        "Color"           => 5, 
                        "Grado"           => 6, 
                        "year"            => 7, 
                        "week"            => 8, 
                        "quantity"        => 9, 
                        "quantity_origin" => 10
                    );


            return (($orden[$a])) < (($orden[$b])) ? -1 : 1;

        }

        $res = [];

        foreach ($array as $arrayInterior) {
            uksort($arrayInterior, "my_sort");
            array_push($res, $arrayInterior);
        }

        print_r($res);

        ?>

Example in PHP Sandbox

    
answered by 15.07.2016 / 02:27
source