How to sort following array with PHP based on its name
's prefix number and produce the output as
1-First
2-Capico
4-Apple
10-Zebra
$array = Array ( [0] => stdClass Object ( [term_id] => 6 [name] => 1-First [slug] => aaa-a [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => category [description] => [parent] => 4 [count] => 2 [cat_ID] => 6 [category_count] => 2 [category_description] => [cat_name] => 1-First [category_nicename] => aaa-a [category_parent] => 4 )
[1] => stdClass Object ( [term_id] => 9 [name] => 10-Zebra [slug] => aaa-f [term_group] => 0 [term_taxonomy_id] => 9 [taxonomy] => category [description] => [parent] => 4 [count] => 1 [cat_ID] => 9 [category_count] => 1 [category_description] => [cat_name] => 10-Zebra [category_nicename] => aaa-f [category_parent] => 4 )
[2] => stdClass Object ( [term_id] => 8 [name] => 2-Capico [slug] => aaa-c [term_group] => 0 [term_taxonomy_id] => 8 [taxonomy] => category [description] => [parent] => 4 [count] => 1 [cat_ID] => 8 [category_count] => 1 [category_description] => [cat_name] => 2-Capico [category_nicename] => aaa-c [category_parent] => 4 )
[3] => stdClass Object ( [term_id] => 7 [name] => 4-Apple [slug] => aaa-b [term_group] => 0 [term_taxonomy_id] => 7 [taxonomy] => category [description] => [parent] => 4 [count] => 1 [cat_ID] => 7 [category_count] => 1 [category_description] => [cat_name] => 4-Apple [category_nicename] => aaa-b [category_parent] => 4 ) )
Tried using
sort($array,SORT_NUMERIC);
foreach ( $array as $single ) {
echo $single->name;
}
but display the output as
4-Apple
2-Capico
10-Zebra
1-First