Jump to content

associative array and foreach


RaiN3772
Go to solution Solved by Psycho,

Recommended Posts

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?

Link to comment
Share on other sites

  • Solution

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>";
}

 

Link to comment
Share on other sites

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 by maxxd
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.