1

If I have a multidimensional array, how can I make it so that certain groups in the array (ex: 'wages', 'hrs' ) will be plugged into a certain formula to become $x, so that it can go through the ifelse statement below?

I'm calculating monthly salary with this general function: ($employees[0]['wage']) * ($employees[0]['hrs']) * 4 However, I want to know how to format the first bracket (key?) $employees[ _ ] , so that every value of the array under the 'hrs' and 'wage' values goes through to calculate what $x would be for EVERY employee in the array (there are 9). I'm assuming I would create a loop, but how would I fill in the bracket to make that happen?

 with monthly salary = ($employees[_]['wage']) * ($employees[_]['hrs']) * 4
 where $employees is a pre-set multidimensional array I created.

<?php 

   $x = __

    if ( $x >= 3000 ) { 
      echo "High paying"; 
     } elseif ( 2000 =< x =< 2999 ) {
        echo "Good paying";
     } else {
      echo "Low paying"; 
     } 
?>  

3 Answers 3

3

You should use this

 $x = array();

for($i=0;$i<count($x);$i++)
{
    if ( $x[$i] >= 3000 ) 
    { 
      echo "High paying"; 
    } 
    elseif ( 2000 >= $x[$i] <= 2999 ) 
    {
       echo "Good paying";
    } 
    else 
    {
      echo "Low paying"; 
     } 
}
Sign up to request clarification or add additional context in comments.

5 Comments

What about since my array is already set with values inside of it? I'm not trying to create an array WITH the equation, I'm trying to make calculations with values already WITHIN a set array and place them INTO the equation mentioned in the text.
@kari this is for only suggestion you can use your array. Kindly check the logic if you are agree then accept the answer by clicking green button
@kari he is using your array, looping through the array. $xis your array, and $i is your index pointer that tells exactly what value is being used. This has nothing to do with constructing a new array.
Ohh that makes more sense. I'm sorry, I'm new to this.
No problem. This is why I am generally against code only answers. One should always leave a minimal explanation as to why the code in question is a valid approach.
1

I think you just want a foreach loop to iterate through each of the employees in your $employees array:

foreach ($employees as $employee) {
    $x = $employee['wage'] * $employee['hrs']) * 4;
    if ( $x >= 3000 ) { 
        echo "High paying"; 
    } elseif ( 2000 =< x =< 2999 ) {
        echo "Good paying";
    } else {
        echo "Low paying"; 
    } 
}

Comments

0

Assuming you are having $employees multi-dimensional array; made below code which gives you salary and it's scale if it is high, low or good.

Here is the code;

/**
 * Function to calculate salary scale is high, good, low
 * @param int $salary
 * @return string
 */
function calculateSalaryScale($salary) {
    if ($salary >= 3000) {
        $scale = "High paying";
    } else if (2000 <= $salary && $salary <= 2999 ) {
        $scale = "Good paying";
    } else {
        $scale = "Low paying";
    }

    return $scale;
}

// Assuming your employyees array is like below
$employees = array(
    array('emp_id' => 1, 'name' => 'John Doe', 'wage' => 90, 'hrs'=> 8),
    array('emp_id' => 2, 'name' => 'Lorem Ipsum', 'wage' => 150, 'hrs'=> 6),
    array('emp_id' => 3, 'name' => 'Doler amit', 'wage' => 50, 'hrs'=> 10),
);

foreach ($employees as &$employee) {
    $employee['salary'] = $employee['wage'] * $employee['hrs'] * 4;
    $employee['scale'] = calculateSalaryScale($employee['salary']);
}

echo "<pre>";
print_r($employees);
exit;

It will give you below output;

Array
(
    [0] => Array
        (
            [emp_id] => 1
            [name] => John Doe
            [wage] => 90
            [hrs] => 8
            [salary] => 2880
            [scale] => Good paying
        )

    [1] => Array
        (
            [emp_id] => 2
            [name] => Lorem Ipsum
            [wage] => 150
            [hrs] => 6
            [salary] => 3600
            [scale] => High paying
        )

    [2] => Array
        (
            [emp_id] => 3
            [name] => Doler amit
            [wage] => 50
            [hrs] => 10
            [salary] => 2000
            [scale] => Good paying
        )

)

You can then proceed with $employees array further accordingly.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.