Get filtered values from array like values between range 10-20

后端 未结 4 404
悲哀的现实
悲哀的现实 2021-01-03 01:00

how to get filtered values from array like values between range 10-20. my array contain 10,15,18,25,88,20. i want to print only between range 10-20.

4条回答
  •  清酒与你
    2021-01-03 01:16

    You can use array_filter

    function range10_20($var) {
        return ( $var >= 10 && $var <= 20 );
    }
    
    $input  = array(10,15,18,25,88,20);
    $output = array_filter($input, 'range10_20');
    

    Or something like that:

    $input  = array(10,15,18,25,88,20);
    $range  = array(
        'from' => 10,
        'to'   => 20
    );
    $output = array_filter($input, function ($element) use ($range) {
        return ( $var >= $range['from'] && $var <= $range['to'] );
    });
    

提交回复
热议问题