phparrayscodeignitermultidimensional-arraycount

Count the number of objects in an array of object with a specified property value


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).


Solution

  •   <?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>