-1

this is my code

<?php 
    $file = fopen("employee.txt","r");
    $under_40=0;
    $salary_sum=0; 
    $emplist=[];
    $total_employees=0;
    echo "Names of all employees whose names end with son : <br/>";
    while(!feof($file))
      {
        $total_employees++;
        $fline=fgets($file);
        list($name, $age, $dept, $salary)= explode(':',$fline);

        if(preg_match("/son$/",$name))
        {
            echo $name."<br/>"; 
        }

        if($age<40)
        {
            $under_40++;
            $salary_sum +=$salary;
            if($salary>40000 )
            {
                $emplist[$name]=$salary;
            }
        }
      }

    if($total_employees>0)
    {
        if($under_40>0)
        {
                $percent=100*$under_40 / $total_employees;
                echo "<br/>Percent of employees under 40 is :".$percent."%<br/>";
                $avg= $salary_sum/$under_40;
                echo "<br/>Averege salary of employees under 40 is :$".$avg."<br/>";
                if(array_keys($emplist))
                {
                    echo "<br/>Sorted list of employees under 40, with salries > \$40000 <br/>";
                    ksort($emplist);
                    echo "<table><tr><td>Name</td><td>Salary</td></tr>";
                    foreach ($emplist as $key=>$value)
                    {
                        echo "<tr><td>".$key."</td><td>\$".$value."</td></tr>";
                    }
                    echo"</table>";
                }
                else
                {
                        echo"There are no employees under 40 who earned over \$40000 <br/>";
                }
        }
        else
        {
                echo"There are no employees under 40 <br/>";
        }
    }
    else
    {
            echo"There are no employees ";
    }
    fclose($file);
?>

expected output is Names of all employees whose names end with son: Johnson Edison Morrison

Percent of employees under 40 is :40%

Averege salary of employees under 40 is :$31000

Sorted list of employees under 40, with salries > $40000 Name Salary Harry $43000

but am getting like this

Names of all employees whose names end with son :

Notice: Undefined offset: 3 in C:\xampp\htdocs\lab2.php on line 12

Notice: Undefined offset: 2 in C:\xampp\htdocs\lab2.php on line 12 Johnson Edison Morrison

Percent of employees under 40 is :50%

Averege salary of employees under 40 is :$21000

Sorted list of employees under 40, with salries > $40000 Name Salary Harry $43000

Aishwarya
  • 7
  • 1
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – B001ᛦ May 08 '20 at 09:15
  • In this case, this means that `explode(':',$fline)` did not return an array with at least four elements, but `list($name, $age, $dept, $salary)=` requires four at this point. So your input data (which you completely neglected to show us) appears to not have at least three `:` in the line that is currently getting processed at this point. – CBroe May 08 '20 at 09:20
  • this is my input: Harry:30:HR:43000 Johnson:28:IT:20000 John Milton:50:Sales:60000 Edison:45:IT:50000 Morrison:46:IT:20000 – Aishwarya May 08 '20 at 09:25

1 Answers1

0

You can complete your array this way :

list($name, $age, $dept, $salary)= array_pad(explode(':',$fline), 4, null);

Rudy
  • 16
  • but if i do so am getting output like this: Names of all employees whose names end with son : Johnson Edison Morrison Percent of employees under 40 is :50% Averege salary of employees under 40 is :$21000 Sorted list of employees under 40, with salries > $40000 Name Salary Harry $43000 – Aishwarya May 08 '20 at 09:32