M
M
Michael R.2015-08-10 18:25:11
PHP
Michael R., 2015-08-10 18:25:11

How to count the number of elements in an array if specific ones are needed?

Hello!
So, there is a task, to count the number of elements in the array, while the count of elements should be exactly those whose height is greater than 173.
- I know how to count count($height);
- And how to set if conditions.
= But here's how to combine everything into one "composition", I'll never know, help pliz. =)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2015-08-10
@Mike_Ro

$array = ;
$filtered = array_filter($array, function ($item) {return $item['height'] >= 3;});
echo count($filtered);

M
Mikhail Osher, 2015-08-10
@miraage

And then count the result.

array_filter(
    $array,
    function($row) {
        return $row['height'] > 173;
    }
);

// UPDATE
$scoolChild = array(
    'Антон'	=>	172,
    'Семен'	=>	165,
    'Лена'	=>	189,
    'Иван'	=>	171,
    'Петр'	=>	182,
    'Сидор'	=>	176,
    'Аня'	=>	180,
    'Таня'	=>	179,
    'Маня'	=>	171
);

$tall = array_filter(
    $scoolChild,
    function($height) {
        return $height > 173;
    }
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question