Adam Posted October 30, 2008 Share Posted October 30, 2008 Hi i've come across a problem in a PHP app I'm writing. I have a multidimensional array which looks like: $breadcrumbs[] = array( 'name' => 'Computers', 'title_order' => 3, ); There's about 6 or 7 $breadcrumbs similar to that. Basically I need to reorder breadcrumbs by 'title_order'. does anybody know how I could do that? Say something like: sort($breadcrumbs['title_order']); Anybody have any ideas? Cheers! Adam Quote Link to comment https://forums.phpfreaks.com/topic/130710-reordering-an-array-by-a-child/ Share on other sites More sharing options...
Adam Posted October 30, 2008 Author Share Posted October 30, 2008 found a soloution.. function sortBreadcrumbs($a, $b) { if ($a['title_order'] == $b['title_order']) { return 0; } elseif ($a['title_order'] < $b['title_order']) { return -1; } else { return 1; } } usort($breadcrumbs, "sortBreadcrumbs"); cheers anyway! Quote Link to comment https://forums.phpfreaks.com/topic/130710-reordering-an-array-by-a-child/#findComment-678302 Share on other sites More sharing options...
zenag Posted October 30, 2008 Share Posted October 30, 2008 use multisort... $breadcrumbs[] = array( 'name' => 'Computers', 'title_order' => 3, ); $breadcrumbs[] = array( 'name' => 'Computers', 'title_order' => 2, ); $breadcrumbs[] = array( 'name' => 'Computers', 'title_order' => 1, ); foreach ($breadcrumbs as $key => $row) { $title_order[] = $row['title_order']; } $sort_order = SORT_ASC; array_multisort($title_order, $sort_order, $breadcrumbs); print_r($breadcrumbs); Quote Link to comment https://forums.phpfreaks.com/topic/130710-reordering-an-array-by-a-child/#findComment-678303 Share on other sites More sharing options...
Adam Posted October 30, 2008 Author Share Posted October 30, 2008 cheers for the reply. Which of our ways is the quickest and/or best? Quote Link to comment https://forums.phpfreaks.com/topic/130710-reordering-an-array-by-a-child/#findComment-678311 Share on other sites More sharing options...
johnake Posted October 30, 2008 Share Posted October 30, 2008 You could try array_multisort: <? $breadcrumbs[] = array( 'name' => 'Computers', 'title_order' => 3, ); $breadcrumbs[] = array( 'name' => 'Printers', 'title_order' => 4, ); $breadcrumbs[] = array( 'name' => 'Misc.', 'title_order' => 5, ); foreach ($breadcrumbs as $key => $row) { $name[$key] = $row['name']; $title_order[$key] = $row['title_order']; } array_multisort($title_order, SORT_ASC, $name, SORT_ASC, $breadcrumbs); var_dump($breadcrumbs); ?> Quote Link to comment https://forums.phpfreaks.com/topic/130710-reordering-an-array-by-a-child/#findComment-678314 Share on other sites More sharing options...
Adam Posted October 30, 2008 Author Share Posted October 30, 2008 cheers guys. Got it sorted in end using array_multisort ! Quote Link to comment https://forums.phpfreaks.com/topic/130710-reordering-an-array-by-a-child/#findComment-678363 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.