1

Here's my problem i have two arrays , one is multi dimensional array the second one is single array, these arrays is displayed in a two table. the multi-array is display in table 1 and single is in table 2. what i want to do is to count the number of employees for each position.

<?php include_once('fake_db.php'); ?>


<h3>Department List</h3>
<table class="table table-condensed table-striped table-bordered table-hover no-margin">
    <thead>
    <tr>
        <td>Name</td><td>Number of Employees</td><td>Action</td>
    </tr>
    </thead>
   <?php foreach($positions as $key => $value){ ?>

        <tr>
            <td><?= $value; ?></td>
            <td></td>
            <td><a href="profile.php?id=<?= $key; ?>">View Employee List</a></td>
        </tr>
    <?php } ?>
</table>

fake_db.php

$employees = array
             (
                 array("name" => 'Jason Alipala', "employee_id" => 'G1001-05', "position" => 1),
                 array("name" => 'Bryann Revina', "employee_id" => 'G1009-03', "position" => 2),
                 array("name" => 'Jeniel Mangahis', "employee_id" => 'G1009-04', "position" => 2),
                 array("name" => 'Arjay Bussala', "employee_id" => 'G1009-05', "position" => 3),
                 array("name" => 'Ronnel Ines', "employee_id" => 'G1002-06', "position" => 3)
             );

$positions = array(1 => 'TL', 2 => 'Programmer', 3 => 'Converter');
2
  • It's not clear exactly what you mean. Do you want to count the number of employees for each position? Commented May 30, 2019 at 3:07
  • Yes , i want to count the number of employees for each position Commented May 30, 2019 at 3:09

1 Answer 1

1

To count the number of employees for each position, you can use array_count_values on the position column of the the employees array. For example:

$emp_positions = array_count_values(array_column($employees, 'position'));
foreach ($positions as $key => $value) {
    echo $positions[$key] . ' : ' . $emp_positions[$key] . ' employees' . PHP_EOL;
}

Output:

TL : 1 employees 
Programmer : 2 employees 
Converter : 2 employees

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

8 Comments

Warning: array_column() expects parameter 1 to be array, null given in C:\xampp\htdocs\gleent\fake_db.php on line 27 Warning: array_count_values() expects parameter 1 to be array, null given in C:\xampp\htdocs\gleent\fake_db.php on line 27
How can i place the result also on my table data ?
@Diether I have used the data from your question (see the demo). I'm not sure why you're seeing that error?
@Diether where do you want to place the results?
I fix now, but how can i place the result in my table data ? not in echo <?php foreach($positions as $key => $value){ ?> <tr> <td><?= $value; ?></td> <td><?= $emp_positions ?></td> <td><a href="profile.php?id=<?= $key; ?>">View Employee List</a></td> </tr> <?php } ?>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.