syed
syed

Reputation: 289

php - print even and (sorted) odd numbers of an array

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; 

Upvotes: 2

Views: 15001

Answers (3)

mickmackusa
mickmackusa

Reputation: 47904

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

Upvotes: 0

alwayS_U
alwayS_U

Reputation: 54

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

Upvotes: 0

Charlotte Dunois
Charlotte Dunois

Reputation: 4680

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

Upvotes: 3

Related Questions