Showing posts with label PHP Comparing Arrays. Show all posts
Showing posts with label PHP Comparing Arrays. Show all posts

Thursday, 30 August 2018

PHP sort array comparing the value and pushing the smaller values ​​last


I have a value and comparing that value i want to change order of an array


for eg. here 3 is value so i need array order to be
5,6,1,2
so values less then 3 shifts last in array.
<?php

$value=3;

$array=array(6,2,5,1);

asort($array);

print_r($array);

?>


Please have a look on the below code, it may help you. You need to use array_filter with call back function to make 2 arrays then u can merge them.
  $value=3;
  $array=array(6,2,5,1);
  asort($array);
  $right = array_filter($array, function($elem) use($value){
      return $elem < $value;
  });
  $left = array_filter($array, function($elem) use($value){
     return $elem > $value;
  });
  //print_r($right);
  //print_r($left);
  $res = array_merge($left,$right);
  print_r($res);