Marcello Impastato - 2 months ago 9
PHP Question

# How order a multidimensional array for multiple key?

``````How order an array so structured for [name] and to parity of [name] for [age] ?

[0] => Array
(
[name] => Mary
[age] => 32
)

[1] => Array
(
[name] => Mary
[age] => 31
)

[2] => Array
(
[name] => Anna
[age] => 30
)

Output:

Name | Age
----------
Anna | 30
Mary | 31
Mary | 32
``````

This is code:

``````function sort(&\$array) {
function build_sorter(\$key) {
return function (\$a, \$b) use (\$key) {
return strnatcmp(\$a[\$key], \$b[\$key]);
};
}
usort(\$array, build_sorter(["name"]));
usort(\$array, build_sorter(["age"]));
}
``````

you can use this function

``````<?php
function Func_Sort(\$arr, \$index) {
\$b = array();
\$c = array();
foreach (\$arr as \$key => \$value) {
\$b[\$key] = \$value[\$index];
}

asort(\$b);

foreach (\$b as \$key => \$value) {
\$c[] = \$arr[\$key];
}

return \$c;
}
\$activities = array (
'1' => array(
'name' => 'Mary',
'age' => 32,

),
'2' => array(
'name' => 'Mary',
'age' => 31,
),
'3' => array(
'name' => 'anna',
'age' => 30
),
);
\$sorted_act = Func_Sort(\$activities, 'age');
echo "<pre>";
print_r(\$sorted_act);

?>
``````