【问题标题】:Calculation using array values in PHP在 PHP 中使用数组值进行计算
【发布时间】:2018-09-21 06:28:50
【问题描述】:

如果我有一个多维数组,我怎样才能使数组中的某些组(例如:'wages'、'hrs')被插入到某个公式中变成 $x,这样它就可以去通过下面的 ifelse 语句?

我正在使用这个通用函数计算月薪: ($employees[0]['wage']) * ($employees[0]['hrs']) * 4 但是,我想知道如何格式化第一个括号(键?)$employees[ _ ],以便“hrs”和“wage”值下的数组的每个值都可以计算出每个员工的 $x数组(有 9 个)。我假设我会创建一个循环,但我将如何填写括号以实现这一点?

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

【问题讨论】:

    标签: php arrays loops if-statement


    【解决方案1】:

    你应该使用这个

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

    【讨论】:

    • 既然我的数组已经在其中设置了值,那该怎么办?我不是试图用方程创建一个数组,我试图用已经在一个集合数组中的值进行计算,并将它们放入文本中提到的方程中。
    • @kari 这仅用于建议您可以使用您的阵列。如果您同意,请检查逻辑,然后单击绿色按钮接受答案
    • @kari 他正在使用你的数组,循环遍历数组。 $x是你的数组,$i是你的索引指针,它准确地告诉你正在使用什么值。这与构造新数组无关。
    • 哦,这更有意义。对不起,我是新手。
    • 没问题。这就是为什么我通常反对 code only 答案。对于为什么有问题的代码是一种有效的方法,人们应该总是留下一个最少的解释。
    【解决方案2】:

    我认为您只需要一个 foreach 循环来遍历您的 $employees 数组中的每个员工:

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

    【讨论】:

      【解决方案3】:

      假设你有$employees 多维数组;下面的代码可以给你薪水,如果它是高、低或好,它的规模。

      这里是代码;

      /**
       * 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;
      

      它会给你下面的输出;

      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
              )
      
      )
      

      然后您可以相应地继续使用$employees 数组。

      【讨论】:

        猜你喜欢
        • 2016-04-18
        • 1970-01-01
        • 2016-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多