首先是下一个,最后一个是基于数字

时间:2016-09-02 06:27:51

标签: php arrays date foreach

我在PHP中有一些像这样的数组

$numbers = array('1','3','4','5','6','7','8','9','0','11','123','89');

当前的数字可以像这样随机

$currentNumber = 32;

我需要的是仅输出低于$currentNumber的数组中的第一个数字以及大于$currentNumber的数组中的第一个更高的数字

我知道我可以输出所有较低的和更大的,只有我需要一个:(

foreach ($numbers as $number){
     if ($number > $currentNumber){
          echo $number .' Number is bigger';
     }
     else{
          echo $number .' Number is lower';
     } 
}

11 个答案:

答案 0 :(得分:1)

你可以这样做:

<?php
$numbers = array('1','3','4','5','6','7','8','9','0','11','123','89');

$currentNumber = 32;

$firstLowerNumber = null;
$firstBiggerNumber = null;

foreach ($numbers as $number) {
    if ($number < $currentNumber && $firstLowerNumber === null) {
        $firstLowerNumber = $number;
    }
    if ($number > $currentNumber && $firstBiggerNumber === null) {
        $firstBiggerNumber = $number;
    }
}

echo $firstBiggerNumber === null ? 'No number is bigger than ' . $currentNumber . PHP_EOL : $firstBiggerNumber . ' Number is bigger' . PHP_EOL;
echo $firstLowerNumber === null ? 'No number is lower than ' . $currentNumber . PHP_EOL : $firstLowerNumber .' Number is lower' . PHP_EOL;

答案 1 :(得分:1)

下面的代码只能找到低于$ currentNumber的数组中的第一个数字,以及大于$ currentNumber的数组中的第一个更高的数字

$firstLowestNum=$firstHigherNum=null;
$currentNumber=32;

$numbers = array('1','3','4','5','6','7','8','9','0','11','123','89');

foreach ($numbers as $number){
 if ($number > $currentNumber && is_null($firstHigherNum)){
  $firstHigherNum =$number;
}
else if(is_null($firstLowestNum)){  
  $firstLowestNum =$number;
}
}
var_dump($firstLowestNum,$firstHigherNum);

答案 2 :(得分:1)

    $numbers = array('1','3','4','5','6','7','8','9','0','11','123','89');
$currentNumber = 32;


$greater_numbers= array();
$lower_numbers= array();
foreach ($numbers as $number){
 if ($number > $currentNumber){

  array_push($greater_numbers,$number);
}
else{
 array_push($lower_numbers,$number);
}
}
sort($greater_numbers);
sort($lower_numbers);
echo "first greater number".$greater_numbers[0];
echo "first lowest number".end($lower_numbers);

答案 3 :(得分:1)

您可以对数组进行升序排序并获得第一个较低元素,它将是此数组中的最低元素。

然后对数组进行降序排序并获得第一个更高的元素,它将是此数组中的最高元素。

$numbers = array('1','3','4','5','6','7','8','9','0','11','123','89');

$input=32;
$max=0;
$min=0;
sort($numbers);
foreach($numbers  as $index=>$value){
    if ($value < $input){
        $min=$value;
        break;
    } 
}

rsort($numbers);
foreach($numbers  as $index=>$value){
    if ($value > $input){
        $max=$value;
        break;
    } 
}

答案 4 :(得分:1)

假设您的$numbers未排序。

$numbers = array('1','3','4','5','6','7','8','9','0','11','123','89');
$currentNumber = 32;

$closestLower = min($numbers);//or some minimum constant as lower bound
$closestHigher = max($numbers);//or some maximum constant as upper bound

foreach($numbers as $number){
    if($number < $currentNumber && $number > $closestLower){
        $closestLower = $number;
    }
    if($number > $currentNumber && $number < $closestHigher){
        $closestHigher = $number;
    }
}
echo $closestLower."\n";
echo $closestHigher."\n";

答案 5 :(得分:0)

first number from array that is lower than $currentNumber我假设您需要在订单中找到的第一个值(如果我们继续前进,可能还有其他值,但您需要第一个值。)

更快的方法是迭代数组两次。获得值后打破循环。

 $currentNumber = 32;
 $min=0; 
 $max=0;
 foreach($numbers as $value){
 if($value<$currentNumber){
    $min = $value;
    break;
   }
 }

foreach($numbers as $value){
  if($value>$currentNumber){
  $max = $value;
  break;
 }
}

答案 6 :(得分:0)

$smaller = $bigger = null;

foreach ($numbers as $number) {
    if ($number < $currentNumber) {
        $smaller = $number;
    }

    if ($number > $currentNumber) {
        $bigger = $number;
        break;
    }
}

echo $smaller, $bigger;

当然,这仅在$numbers数组已排序时才有效。如果无法保证,请先使用sort功能。

答案 7 :(得分:0)

假设数组已排序,只需使用两个标志,指示您是否找到了较低和较高的数字。无需迭代数组两次。

    $lowerNumberFound= false;
    $higherNumberFound = false;
    foreach ($numbers as $number){
     if (!higherNumberFound && $number > $currentNumber) {
         echo $number .' Number is bigger';
         $higherNumberFound = true;
    }
    else if(!lowerNumberFound && $number < $currentNumber {
        echo $number .' Number is lower';
        $lowerNumberFound= true;
    }

答案 8 :(得分:0)

以下方式使用

$numbers = array('1','3','4','5','6','7','8','9','0','11','123','89');

$currentNumber = 32;

sort($numbers);
foreach($numbers as $key=>$row){
    if($currentNumber <$row){
        break;
    }

}
echo 'lower Number = '. $numbers[$key==2?$key-2:$key-1].'<br>';
echo 'Greater Number = '. $numbers[$key].'<br>';

如果相同,则检查具有当前值的最后一个值,它需要更多的扭曲

答案 9 :(得分:0)

<?php

    $numbers = array('1','3','4','5','6','7','8','9','0','11','123','89');

    $currentNumber =7;
    $low_number=$high_number= false;

        foreach ($numbers as $number){

         if (!$high_number && $number > $currentNumber) {
             echo $number .' Number is bigger</br>';
             $high_number = true;
        }
        else if(!$low_number && $number < $currentNumber) {
            echo $number .' Number is lower</br>';
            $low_number= true;
        }

        }
    ?>

更新1

        <?php

        $numbers = array('1','3','4','5','6','7','8','9','0','11','123','89');

        $currentNumber =7;

       $new = array_flip($numbers);

            foreach ($new  as $key=>$number){

                    if($key==$currentNumber)
                    {

                        echo prev($new)."Number is lower</br>";

                        echo next($new)."Number is bigger</br>";

                        break;
                    }

            }

    ?>

答案 10 :(得分:0)

$numbers = array('1','3','4','5','6','7','8','9','0','11','123','89');
$currentNumber=32;

sort( $numbers );
while( list( $key, $value ) = each( $numbers ) ){
    if( $currentNumber > $value ){
        $low=$value;
        break;
    }
}
while( list( $key, $value ) = each( $numbers ) ){
    if( $currentNumber < $value ){
        $high=$value;
        break;
    }
}
echo 'Low:'.$low.', High:'.$high;