I have code like this:
<?php if(isset($global_info_results)): ?>
<?php echo count($global_info_results) ?>
<span>Mali Oglasi: </span>
<?php foreach ($global_info_results as $result) : ?>
<?php if($result->info_type_id == 1) : ?>
<p><?php echo $result->name?></p>
<?php endif ?>
<?php endforeach; ?>
<?php endif ?>
How can I count specific value inside array (for example I want to count how much result have info_type_id == 1
).
<?php $a = 0
foreach ($global_info_results as $result)
if($result->info_type_id == 1)
{ $a = $a + 1}
End Foreach?>
<span>Mali Oglasi: </span>
<?php foreach ($global_info_results as $result) : ?>
<?php if($result->info_type_id == 1) : ?>
<p><?php echo $result->name?></p>
You could use array_filter to generate an array of values that match the criteria you want a count of and then run count on the result. The below example returns the number of elements in an array that have a value greater than 4:
$items = array (1, 2, 3, 4, 5, 6, 7, 8, 9);
$itemsOfInterest = array_filter ($items, function ($elem) {return ((int) $elem > 4);})
echo (count ($itemsOfInterest));