cobraroll98 Posted October 8, 2008 Share Posted October 8, 2008 Hello, I have an array which contains the values: 1,2,3,4,5,5,4,3,2,1,1,2,3,4,5,3,2 Assume each array value is a number 1-1000, and is not pushed into the array in any relevant order. I would like to display the array values in the following order: 1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,2,3 I have had some difficulty writing/finding a function which will order things in this fashion. Ideally I want to use this function to display results from a Postgres database but since the db's result set returns an array I figured this was a good place to start. I'm not a very good math person, just a functional programmer, so any help would be awesome! Thanks in advance. Martin. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2008 Share Posted October 8, 2008 and what are the rules dictating that 1,2,3,4,5,5,4,3,2,1,1,2,3,4,5,3,2 should become 1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,2,3 Quote Link to comment Share on other sites More sharing options...
cobraroll98 Posted October 9, 2008 Author Share Posted October 9, 2008 As far as rules I'm afraid I'm not sophisticated enough to know what you're asking. Perhaps this helps: The each resulting array value should not be the same as the value before or after it unless there are no alternatives. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 9, 2008 Share Posted October 9, 2008 I asked because from your example it could also be min - max, min - max, min - max, whats_left or split into groups of five and reverse alternate groups Quote Link to comment Share on other sites More sharing options...
sasa Posted October 9, 2008 Share Posted October 9, 2008 try <?php function my_order($a){ if (!is_array($a)) return false; $out = array(); while (count($a)){ $tmp = array_unique($a); sort($tmp); $out = array_merge($out, $tmp); foreach ($tmp as $val){ unset($a[array_search($val,$a)]); } } return $out; } $start = '1,2,3,4,5,5,4,3,2,1,1,2,3,4,5,3,2'; $start = explode(',',$start); echo implode(',', my_order($start)); ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 9, 2008 Share Posted October 9, 2008 OK, I thought this was an interesting problem, so I decided to solve it another way. Here's another solution - don't know if it is more or less efficient than Sasa's function collate($arry) { if (!is_array($arry)) return false; $out = array(); $values = array_count_values($arry); ksort($values); while (count($values)) { $out = array_merge($out, array_keys($values)); foreach($values as $key => $value) $values[$key]--; $values = array_filter($values); } return $out; } Quote Link to comment Share on other sites More sharing options...
cobraroll98 Posted October 10, 2008 Author Share Posted October 10, 2008 sasa, mjdamato, Thank you so much for the help your functions work great! Is there an easy way to perform this on a multidimensional array specifying the array 'column' that contains the number to be sorted? mjdamato: It seems the array_count_values function doesn't work with multid arrays so I've been looking for a substitute. Here's an example of my multiD array, I would like to perform the collate function based on the value of the ID column. Thank you so much once again! $start =array(); array_push($start,array("ID" => 1, "Name" => Fish)); array_push($start,array("ID" => 2, "Name" => dog)); array_push($start,array("ID" => 3, "Name" => boat)); array_push($start,array("ID" => 4, "Name" => hat)); array_push($start,array("ID" => 5, "Name" => smurf)); array_push($start,array("ID" => 5, "Name" => toad)); array_push($start,array("ID" => 5, "Name" => princess)); Quote Link to comment Share on other sites More sharing options...
sasa Posted October 10, 2008 Share Posted October 10, 2008 try <?php $start =array(); array_push($start,array("ID" => 1, "Name" => Fish)); array_push($start,array("ID" => 2, "Name" => dog)); array_push($start,array("ID" => 5, "Name" => boat)); array_push($start,array("ID" => 4, "Name" => hat)); array_push($start,array("ID" => 3, "Name" => smurf)); array_push($start,array("ID" => 2, "Name" => toad)); array_push($start,array("ID" => 5, "Name" => princess)); //reorganize array $new_start = array(); foreach ($start as $elemen){ $new_start[$elemen['ID']][] = $elemen; } $out = array(); ksort($new_start); while (count($new_start) and $i++ <11){ foreach ($new_start as $id => $arr_elem){ array_push($out, array_shift($new_start[$id])); if (count($new_start[$id]) == 0) unset($new_start[$id]); } } print_r($out); ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 10, 2008 Share Posted October 10, 2008 sasa, mjdamato, Thank you so much for the help your functions work great! Is there an easy way to perform this on a multidimensional array specifying the array 'column' that contains the number to be sorted? mjdamato: It seems the array_count_values function doesn't work with multid arrays so I've been looking for a substitute. Here's an example of my multiD array, I would like to perform the collate function based on the value of the ID column. Thank you so much once again! $start =array(); array_push($start,array("ID" => 1, "Name" => Fish)); array_push($start,array("ID" => 2, "Name" => dog)); array_push($start,array("ID" => 3, "Name" => boat)); array_push($start,array("ID" => 4, "Name" => hat)); array_push($start,array("ID" => 5, "Name" => smurf)); array_push($start,array("ID" => 5, "Name" => toad)); array_push($start,array("ID" => 5, "Name" => princess)); I wouldn't set up my array like that, to be honest. array_push() is kind of slow compared to just using the [] notation: $start = array(); $start[] = array('ID' => 4, 'Name' => 'hat'); //etc Quote Link to comment 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.