2

I have an array given below

$array = array(50,51,52,53,54,55,56,57,58,59);

I am trying to print the values of array while even numbers will remain in same order and the odd numbers are sorted i.e. 59,57,55,53,51

The output should be like

50,59,52,57,54,55,56,53,58,51

I've separated the even and odd numbers in two diff variables. how should i proceed further ?

here is my code

  $even= "";
  $odd= "";

for($i=50;$i<=59;$i++)
{
    if($i%2==0)
    {
        $even.=$i.",";
    }else $odd.=$i.","; 
}   
echo $even.$odd; 
5
  • Can you share what you've tried? Commented Aug 22, 2016 at 17:58
  • i've edited my post #Jay Commented Aug 22, 2016 at 18:00
  • you want to sort based on number or based on index? Commented Aug 22, 2016 at 18:07
  • sort only odd numbers, from 59-57-55-53-51 #Yasin Commented Aug 22, 2016 at 18:09
  • Something like this should work for you: 3v4l.org/MRu43 basically you get the odd values out of the array and sort them. After that you just replace the odd values from your original array with the sorted odd values. Commented Aug 22, 2016 at 18:18

3 Answers 3

3

Instead of pushing the evens and odds into a string, push them each into an array, sort the array with the odds in reverse and then loop through one of them (preferably through the even array) and add the even and the odd to a new array.

This is how I've done it:

$array = array(50,51,52,53,54,55,56,57,58,59);
$odds = array();
$even = array();
foreach($array as $val) {
    if($val % 2 == 0) {
        $even[] = $val;
    } else {
        $odds[] = $val;
    }
}

sort($even);
rsort($odds);

$array = array();
foreach($even as $key => $val) {
    $array[] = $val;
    if(isset($odds[$key])) {
        $array[] = $odds[$key];
    }
}

https://3v4l.org/2hW6T

But you should be cautious if you have less even than odd numbers, as the loop will finish before all odds are added. You can check for that either before or after you've filled the new array. If you check after filling the new array, you can use array_diff and array_merge to add the missing odds to the new array.

http://php.net/array_diff http://php.net/array_merge

Sign up to request clarification or add additional context in comments.

6 Comments

This will only work as long as odd and even values are in alternating order.
@Rizier123 Why should it only work if they're in alternating order?
@CharlotteDunois For example if there are two odd or even values besides each other in the array it will break: 3v4l.org/lN4aj
@Rizier123 I don't see anything broken. It's in the intended order.
This answer is only appropriate if the input array alternates from even and odd every time. The loop after sorting is blindly zippering the values back together.
|
0

Following code will run as per the odd number count. There won't be any loop count issue.

  function oddNumber($num){
    return $num % 2 == 1;
  }

  $givenArray = array(50, 51, 52, 53, 54, 55, 56, 57, 58, 59);

  $oddArray = array_filter($givenArray, "oddNumber");

  rsort($oddArray);

  $finalArray = array();
  $oddCount = 0;
  foreach($givenArray as $num){
      if(oddNumber($num)){    
          $finalArray[] = $oddArray[$oddCount];
          ++$oddCount;
      }else{
          $finalArray[] = $num;    
      }
  }

  print_r($finalArray);

Comments

0

Isolate the odds values in a new array, preserve the keys of those odds values, sort the odds values descending, reinsert the sorted values into the original array at the cached keys. Demo

$odds = [];
foreach ($array as $i => $v) {
    if ($v & 1) {
        $odds[$i] = $v;
    }
}
$keys = array_keys($odds);
rsort($odds);
foreach ($keys as $i => $k) {
    $array[$k] = $odds[$i];
}
var_export($array);

Or this variation. Demo

$odds = [];
foreach ($array as $i => $v) {
    if ($v & 1) {
        $keys[] = $i;
        $odds[] = $v;
    }
}
rsort($odds);
var_export(
    array_replace(
        $array,
        array_combine($keys, $odds)
    )
);

To spare you the effort of trying to sort values directly by reference, THIS WILL NOT provide the desired mutation:

$odds = [];
foreach ($array as &$v) {
    if ($v & 1) {
        $odds[] =& $v;
    }
}
rsort($odds);
var_export($array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.