-4

If I have an indexed PHP array:

$array = array(2, 12, 23, 34, 36, 36, 36, 56);

And I have a variable:

$number = 30;

How do I calculate the number of "array positions" that are higher than $number?

For example, the function should return 5, because there are 5 numbers (34, 36, 36, 36 and 56) that are higher than 30.

Edit: I've already tried different methods of counting the array values, but I have no idea how to count the positions. It has me stumped.

TinyTiger
  • 1,801
  • 7
  • 47
  • 92
  • Please show us what you have tried so far. – Christophe Weis Jun 25 '18 at 05:40
  • `foreach` and `if`? – M. Eriksson Jun 25 '18 at 05:41
  • Please read: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and also [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – M. Eriksson Jun 25 '18 at 05:44
  • could be considered a dupe of https://stackoverflow.com/questions/25181798/how-can-i-include-a-variable-inside-a-callback-function but I dont wanna dupehammer it since it's fixed on array_reduce. – Gordon Jun 25 '18 at 06:06
  • you should explain, what have you tried so far, as we are here to help, we are not your personal developers. We are **not here to provide you with a code** suitable for the situation. – Code_Ninja Jun 25 '18 at 06:30

7 Answers7

1

You can use array_reduce for this task:

$array = array(2, 12, 23, 34, 36, 36, 36, 56);
$number = 30;

$cnt = array_reduce($array, function ($carry, $item) use ($number) {
  if ( $item > $number ) {
    $carry++;
  }
  return $carry;
});

echo $cnt;
napuzba
  • 6,033
  • 3
  • 21
  • 32
1

Can also use

  • array_reduce — Iteratively reduce the array to a single value using a callback function

Example:

$array = array(2, 12, 23, 34, 36, 36, 36, 56);
$number = 30;

echo array_reduce($array, function($ret, $val) use ($number) {
    return $ret += $val > $number;
});

Or hardcode the number into the callback

echo array_reduce($array, function($ret, $val) {
    return $ret += $val > 30;
});
Gordon
  • 312,688
  • 75
  • 539
  • 559
1

As another alternative, use array_filter() to remove the elements from the array that you don't want, then just count() the remaining list...

$array = array(2, 12, 23, 34, 36, 36, 36, 56);
$number = 30;

$gtn = array_filter($array, function($value) use ($number) { return $value > $number; });
echo count($gtn); 

$gtn will contain the numbers (34, 36, 36, 36, 56), which may be useful for other things in your code.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
1

You don't need to iterate the full array.
If you first sort the array then as soon as you find a larger number you can break the loop and use the key value from there.

$array = array(2, 12, 23, 34, 36, 36, 36, 56);
Sort($array);
$number = 30;

Foreach($array as $key => $val) If($val >= $number) break;

$larger = array_slice($array, $key);

Echo count($larger) . "\n";
Var_dump($larger);

Output:

5 //echo
array(5) { // var_dump
  [0] => 34
  [1] => 36
  [2] => 36
  [3] => 36
  [4] => 56
}

https://3v4l.org/8FoOP

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

Try this code:

function findGreaterNumberCount($array, $number) {
    $count = 0;
    foreach ($array as $value) {
        if ($value > $number) {
            $count++;
        }
    }
    return $count;
}

echo findGreaterNumberCount(array(2, 12, 23, 34, 36, 36, 36, 56), 30);

This platform doesn't recommend to write code. So you should try something before asking a question. Be careful next time.

Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36
  • 2
    In my opinion, any question not showing any effort at all shouldn't be answered. That basically reduces this sit to a free coding service, which it is not. – M. Eriksson Jun 25 '18 at 05:42
  • 2
    This platform also requires answers to include a description, not just "try this" – Andreas Jun 25 '18 at 06:23
0

You can iterate array with array_filter to find the greatest array element.

$num = 30;
$array = array(2, 12, 23, 34, 36, 36, 36, 56);
    $a = array_filter($array, function($value) use( $num ){
            return $num < $value;
  });
 print_r($a);

Out put

   Array
    (
        [3] => 34
        [4] => 36
        [5] => 36
        [6] => 36
        [7] => 56
    )
Shivrudra
  • 674
  • 4
  • 5
  • @SambhajiKatrajkar Code only answers are frowned upon at SO. Writing just "Try this" as some do is not a proper description of the code. In this specific answer a good description could be *"You can use array_filter and make it return values larger than 30"*. But just not saying anything makes the answer a code only answer and some find them not useful, thus downvote them. https://meta.stackoverflow.com/questions/345719/low-quality-posts-and-code-only-answers – Andreas Jun 25 '18 at 07:56
  • I'm quite sure array_filter works iterative, so adding $num inside the function means it's reset on every item of the array. – Andreas Jun 25 '18 at 07:58
0

use array reduce and make your code short

    $array = [2, 12, 23, 34, 36, 36, 36, 56];
        $number = 30;

        $totalNumberOfItems = array_reduce($array, function ($sum, $item) use ($number) {
            if($item > $number)
            $sum++;
          return $sum;
        });

Var_dump($cnt);
Marvin Collins
  • 379
  • 6
  • 14