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