I’m struggling to sort my array of objects into alphabetical order by the title key. Getting error that ‘title’ is undefined.
I want to go from this:
My Array
(
[0] => Array
(
[area] => TACT
[pages] => Array
(
[0] => Array
(
[id] => 1484
[title] => Registry
)
[1] => Array
(
[id] => 1385
[title] => Education
To:
My Array
(
[0] => Array
(
[area] => TACT
[pages] => Array
(
[0] => Array
(
[id] => 1385
[title] => Education
)
[1] => Array
(
[id] => 1484
[title] => Registry
Have tried:
function sort_alpha_title($a, $b) {
return strnatcmp($a['title'], $b['title']);
}
usort($myArray, 'sort_alpha_title');
2
Answers
You should use
Because the title property is a child property of the pages property.
Sry for the doubled answer but i cant add a comment because of my reputation.
tomhuckle wrote
you dont need the foreach-loop (at the end of your comment) because its already "looped" within one call.
best regards