waverider303 Posted October 3, 2010 Share Posted October 3, 2010 I am looking for information on how to accomplish this: I want to order an array based on a variable. I have an array that is set like this array(1,2,3,4,5,6). I want to order this array base on a variable that will be in the array. So say my variable $x = 3, I want to order the array starting with the number right after the $x. So the array would look like this now array(4,5,6,1,2,3). Is this even possible? Another example if $x = 6, The array would be array(1,2,3,4,5,6). So basically order the array so that the set variable (in this case $x) will be at the end and the rest will run in order. Quote Link to comment https://forums.phpfreaks.com/topic/215017-php-sorting-issue/ Share on other sites More sharing options...
harristweed Posted October 3, 2010 Share Posted October 3, 2010 <?php $the_array=array("1","2","3","4","5","6"); $x=3; $first_part=array(); $second_part=array(); foreach ($the_array as $value) { if($value <= $x){ $second_part[]=$value; }else{ $first_part[]=$value; } $sorted_array=array_merge($first_part, $second_part); } foreach ($sorted_array as $value) { echo"$value<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/215017-php-sorting-issue/#findComment-1118492 Share on other sites More sharing options...
PaulRyan Posted October 3, 2010 Share Posted October 3, 2010 Harristweed, your code does work until you put mutliple numbers or the same value into it. This will work, I believe so anyhow... <?php // Starting Array $myArray = array('1','2','5','6','8','3','4','2','5'); // Count length of array $count = count($myArray); // Number to end at $endAt = 5; // If max number in array is less than the set value // set end value to max number if(max($myArray) < $endAt) { $endAt = max($myArray); } // Create 2 arrays to hold the sorted data $lowerThan = array(); $higherThan = array(); // Push the above arrays to contain values either high than the $endAt value // Or push to contain lower than or equal value for($e=0; $e<$count; $e++) { if($myArray[$e] <= $endAt) { array_push($lowerThan, $myArray[$e]); } else { array_push($higherThan, $myArray[$e]); } } // Sort the arrays in numerical order sort($lowerThan); sort($higherThan); // Merga arrays in correct format $result = array_merge($higherThan, $lowerThan); // Print the result print_r($result ?> Tell me how it goes waverider303 Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/215017-php-sorting-issue/#findComment-1118494 Share on other sites More sharing options...
waverider303 Posted October 3, 2010 Author Share Posted October 3, 2010 Thanks Paul!, This works the only issue I run into is I have an array that is like the following: Array ( [0] => Array ( [episode] => 1 [season] => 1 [id] => 3 [title] => "episode 1" ) [1] => Array ( [episode] => 2 [season] => 1 [id] => 4 [title] => "episode 2" ) [2] => Array ( [episode] => 3 [season] => 1 [id] => 5 [title] => "episode 3" ) [3] => Array ( [episode] => 5 [season] => 1 [id] => 7 [title] => "episode 5" ) [4] => Array ( [episode] => 6 [season] => 1 [id] => 8 [title] => "episode 6" ) ) And I want to sort it by the episode number. Quote Link to comment https://forums.phpfreaks.com/topic/215017-php-sorting-issue/#findComment-1118600 Share on other sites More sharing options...
PaulRyan Posted October 3, 2010 Share Posted October 3, 2010 Yeah that is an issue, I'll have a play about with my code to see if I can get it to work Thanks, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/215017-php-sorting-issue/#findComment-1118611 Share on other sites More sharing options...
BlueSkyIS Posted October 3, 2010 Share Posted October 3, 2010 maybe use usort() with a custom function. function compare_episodes($a, $b) { return strcmp($a['episode'], $b['episode']); } $data = array( array('episode' => 1,'season' => 1,'id' => 3,'title' => "episode 1"), array('episode' => 2,'season' => 1,'id' => 4,'title' => "episode 2"), array('episode' => 3,'season' => 1,'id' => 5,'title' => "episode 3"), array('episode' => 4,'season' => 1,'id' => 7,'title' => "episode 5"), array('episode' => 5,'season' => 1,'id' => 8,'title' => "episode 6") ); usort($data, 'compare_episodes'); print_r($data); Quote Link to comment https://forums.phpfreaks.com/topic/215017-php-sorting-issue/#findComment-1118616 Share on other sites More sharing options...
waverider303 Posted October 10, 2010 Author Share Posted October 10, 2010 Hey Paul! I figured this out just using a foreach loop. I basically.... foreach ($data as $k => $v) { if($data[$k]['episode'] <= $currVideo) { $move = $data[$k]; unset($data[$k]); $data[] = $move; } } Quote Link to comment https://forums.phpfreaks.com/topic/215017-php-sorting-issue/#findComment-1120862 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.