RaiN3772 Posted October 27, 2021 Share Posted October 27, 2021 Hello there, i have this class assignment which im struggling with, im not even sure of the assignment is correct, so basically the assignment is: https://prnt.sc/1xidzwc im struggling with associative array, (still im pretty sure we have to use multidimensional array) and foreach, how im supposed to dislpay it so here is what i ended up with <?php /* $employees = array ( array("Employee-1", "IT", 5000), array("Employee-2", "HR", 4000), array("Employee-3", "Marketing", 3000), array("Employee-4", "Sales", 1500), array("Employee-5", "Somewhere", 4600), array("Employee-6", "Here", 3700) ); */ $name = array("Employee-1", "Employee-2", "Employee-3", "Employee-4", "Employee-5", "Employee-6"); $department = array("IT", "Sales", "Marketing", "HR", "Here", "There"); $salary = array(5000, 4000, 4500, 3000, 1500, 3600); $table = array("Employee Name" => $name, "Department" => $department, "Salary"=>$salary); ?> <table class="table"> <thead> <tr> <th>Employee Name</th> <th>Department</th> <th>Salary</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> </tbody> </table> so what i tried is to use foreach and nested loop foreach ($table as $k => $v) { echo "<tr>"; for ($a=0; $a < count($v); $a++){ echo "<td>" . $v[$a] . "</td>"; } echo "</tr>"; } but it displays the output as rows not the names in columns and the info in the other side, second even if it works still its not what the professor asked for, is there anything im missing here? Quote Link to comment https://forums.phpfreaks.com/topic/314126-associative-array-and-foreach/ Share on other sites More sharing options...
gw1500se Posted October 27, 2021 Share Posted October 27, 2021 $v is the value coinciding with the key, $k. echo "<td>" . $v . "</td>"; Quote Link to comment https://forums.phpfreaks.com/topic/314126-associative-array-and-foreach/#findComment-1591487 Share on other sites More sharing options...
Solution Psycho Posted October 27, 2021 Solution Share Posted October 27, 2021 I am pretty certain that your source array is not constructed how the instructor intended. You are simply assigning an array of values to each associative key. I would think the source array should have an element for each employee with each value assigned to an associative key. Something like this: $employees = array ( array('name' => 'Employee-1', 'department' -> 'IT', 'salary' => '5000'), array('name' => 'Employee-2', 'department' -> 'HR', 'salary' => '4000'), array('name' => 'Employee-3', 'department' -> 'Marketing', 'salary' => '3000'), array('name' => 'Employee-4', 'department' -> 'Sales', 'salary' => '1500'), // etc. . . . ); The loop over each record in the $employees array foreach($employees as $employee) { echo "Name: {$employee['name']}<br>"; echo "Department: {$employee['department']}<br>"; echo "Salary: {$employee['salary']}<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/314126-associative-array-and-foreach/#findComment-1591489 Share on other sites More sharing options...
maxxd Posted October 29, 2021 Share Posted October 29, 2021 (edited) It could just be nested indexed arrays. So instead of foreach($employees as $employee) { echo "Name: {$employee['name']}<br>"; echo "Department: {$employee['department']}<br>"; echo "Salary: {$employee['salary']}<br>"; } it would simply be foreach($employees as $employee) { echo "Name: {$employee[0]}<br>"; echo "Department: {$employee[1]}<br>"; echo "Salary: {$employee[2]}<br>"; } Not the best pattern or most readable code, but if it's early days in the class it could get a lesson about array access across. Edit: this is assuming the array declaration in the initial comment is from the instructor as the system input. Edited October 29, 2021 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/314126-associative-array-and-foreach/#findComment-1591526 Share on other sites More sharing options...
Psycho Posted October 29, 2021 Share Posted October 29, 2021 40 minutes ago, maxxd said: It could just be nested indexed arrays. So instead of The lesson instructions state to create an associative array. Quote Link to comment https://forums.phpfreaks.com/topic/314126-associative-array-and-foreach/#findComment-1591528 Share on other sites More sharing options...
maxxd Posted October 29, 2021 Share Posted October 29, 2021 26 minutes ago, Psycho said: The lesson instructions state to create an associative array. Missed that entirely - my bad. Quote Link to comment https://forums.phpfreaks.com/topic/314126-associative-array-and-foreach/#findComment-1591529 Share on other sites More sharing options...
RaiN3772 Posted October 29, 2021 Author Share Posted October 29, 2021 On 10/27/2021 at 8:49 PM, Psycho said: I am pretty certain that your source array is not constructed how the instructor intended. You are simply assigning an array of values to each associative key. I would think the source array should have an element for each employee with each value assigned to an associative key. Something like this: $employees = array ( array('name' => 'Employee-1', 'department' -> 'IT', 'salary' => '5000'), array('name' => 'Employee-2', 'department' -> 'HR', 'salary' => '4000'), array('name' => 'Employee-3', 'department' -> 'Marketing', 'salary' => '3000'), array('name' => 'Employee-4', 'department' -> 'Sales', 'salary' => '1500'), // etc. . . . ); The loop over each record in the $employees array foreach($employees as $employee) { echo "Name: {$employee['name']}<br>"; echo "Department: {$employee['department']}<br>"; echo "Salary: {$employee['salary']}<br>"; } i ended up doing this, thank you so much for your help the final code is: $employees = array ( array('name' => 'Employee-1', 'department' => 'IT', 'salary' => 5000), array('name' => 'Employee-2', 'department' => 'HR', 'salary' => 4000), array('name' => 'Employee-3', 'department' => 'Marketing', 'salary' => 3000), array('name' => 'Employee-4', 'department' => 'Sales', 'salary' => 1950), array('name' => 'Employee-5', 'department' => 'Management', 'salary' => 1700), array('name' => 'Employee-6', 'department' => 'Finance', 'salary' => 1500) ); function getTotalSalaries() { $totalSalaries = 0; global $employees; // We want to use a global variable from outside the function foreach ($employees as $value) { $totalSalaries += $value['salary']; } return $totalSalaries; } function getMaxSalary() { global $employees; $maxSalary = max(array_column($employees, 'salary')); // Get the maximum value from the returned values in a single column (salary) in the input array return $maxSalary; } Results: https://imgur.com/GAxkzzE Quote Link to comment https://forums.phpfreaks.com/topic/314126-associative-array-and-foreach/#findComment-1591551 Share on other sites More sharing options...
Barand Posted October 29, 2021 Share Posted October 29, 2021 Avoid using "global". Instead, pass the $employees array as an argument to functions. EG echo getTotalSalaries($employees); function getTotalSalries($emps) { return array_sum(array_column($emps, 'salary')); } 1 Quote Link to comment https://forums.phpfreaks.com/topic/314126-associative-array-and-foreach/#findComment-1591574 Share on other sites More sharing options...
RaiN3772 Posted November 1, 2021 Author Share Posted November 1, 2021 On 10/29/2021 at 10:20 PM, Barand said: Avoid using "global". Instead, pass the $employees array as an argument to functions. EG echo getTotalSalaries($employees); function getTotalSalries($emps) { return array_sum(array_column($emps, 'salary')); } make sense, thanks alot for the help Quote Link to comment https://forums.phpfreaks.com/topic/314126-associative-array-and-foreach/#findComment-1591652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.