fiasst Posted November 9, 2010 Share Posted November 9, 2010 Hi, I've been trying to re-order an array and I can't seem to even do it let alone neatly. I've ran out of time and would appreciate any help! $array = array( '0' => array( 'filepath' => 'files/image_site1.png', 'title' => 'Website', ), '1' => array( 'filepath' => 'files/image_id.png', 'title' => 'Identitity', ), '2' => array( 'filepath' => 'files/image_site2.png', 'title' => 'Website', ) ) I'm looking to re-order the arrays within the parent array depending on whether the 'filepath's contain a sub string of '_id' or '_site'. I've been using: $type = '_site'; foreach ($array as $item){ if (preg_match("/$type/i", $item['filename'])){ //add this $item to the top of array, somehow... } } So with $type set to '_site', I'd like to re-arrange the array so both arrays containing _site1.png and _site2.png appear at the top of the parent array. I.e: $array('0' = array(...), '2' => array(...), '1' => array(...)); I hope I've explained it well enough. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/218187-best-way-to-re-order-this-array/ Share on other sites More sharing options...
salathe Posted November 9, 2010 Share Posted November 9, 2010 There are a number of different things that you could do. An idea would be to split apart the different arrays (e.g. one containing just _site items, another containing just _id items, and the left-overs), sort those individually and then put them all back into one big array in whichever order you want. Another idea is to use the existing array sorting functions which allow you to provide a custom comparison function to determine what gets sorted "higher" than other values. To give you an example, this sorts your array alphabetically by filepath but pushes the _site items to the top (also in alphabetical order). uasort($array, 'sorter'); print_r($array); function sorter($a, $b) { $a_site = strpos($a['filepath'], '_site') !== FALSE; $b_site = strpos($b['filepath'], '_site') !== FALSE; if ($a_site && !$b_site) { // Only $a is site, move $a up return -1; } elseif (!$a_site && $b_site) { // Only $b is site, move $a down return 1; } else { // Both site, or both not site, compare alphabetically return strcmp($a['filepath'], $b['filepath']); } } The basic idea of a comparison function like that is to return less than zero (can be anything but is traditionally -1) if the first value is less than the second value (think: it is "lighter" so floats towards the top), zero if they are both considered the same, and greater than zero otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/218187-best-way-to-re-order-this-array/#findComment-1132167 Share on other sites More sharing options...
fiasst Posted November 9, 2010 Author Share Posted November 9, 2010 Blimey, never would have come to that conclusion. Thankyou, I'll have a play with it! Quote Link to comment https://forums.phpfreaks.com/topic/218187-best-way-to-re-order-this-array/#findComment-1132172 Share on other sites More sharing options...
salathe Posted November 9, 2010 Share Posted November 9, 2010 It can take a while to get used to, do come back if you have trouble. Quote Link to comment https://forums.phpfreaks.com/topic/218187-best-way-to-re-order-this-array/#findComment-1132178 Share on other sites More sharing options...
fiasst Posted November 9, 2010 Author Share Posted November 9, 2010 Having had a long think I came up with this which seems to work too! <?php $arr = array( 0 => array('filename' => 'image_id.jpg'), 1 => array('filename' => 'image_site1.jpg'), 2 => array('filename' => 'image_site2.jpg'), ); $type = "_site"; for ($i = 0; $i < count($arr); $i++){ $key = ((preg_match("/$type/i", $arr[$i]['filename'])) ? -$i : $i); $newarr[$key] = $arr[$i]['filename']; } ksort($newarr); ?> Not sure how clean a solution it is though. Quote Link to comment https://forums.phpfreaks.com/topic/218187-best-way-to-re-order-this-array/#findComment-1132426 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.