Jump to content

PHP sorting issue


waverider303

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/215017-php-sorting-issue/
Share on other sites

<?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";
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/215017-php-sorting-issue/#findComment-1118492
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/215017-php-sorting-issue/#findComment-1118494
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/215017-php-sorting-issue/#findComment-1118600
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/215017-php-sorting-issue/#findComment-1118616
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.