doucheng4660 2018-01-30 00:13
浏览 34
已采纳

当foreach中的函数遇到一个非常好的数值

For some reason I keep getting this notice when I try to loop through an array and change it's values.

For some reason, the array is passing 2 values, the one before and the one after when running the same function that changes the values.

class UnitConverter {

    public $vulgar_to_float = array('½' => '1/2');

    public function replaceUnicode($amount){
        foreach($this->vulgar_to_float as $key => $float){
            if(is_numeric($amount)){
                return $amount;
            } else if($key === $amount){
                return $float;
            }
        }
    }

    public function convertAmount($amount, $from, $to){
        if($from === 'pound' && $to === 'ounce'){
            return $amount * 16;
        } else if($from === 'cup' && $to === 'tablespoon'){

            print_r($amount); // here it's echoing 2 values when it should be 1

            return $this->replaceUnicode($amount) * 16;
        } else {
            throw new \Exception('Unable to convert ' . $from . ' to ' . $to);
        }
    }
}


function convertIngredients($arr){
    foreach($arr as $key => $value){
        if($arr[$key]['unit_name'] === 'pound'){
            $arr[$key]['amount'] = (new UnitConverter())->convertAmount($arr[$key]['amount'], 'pound', 'ounce');
            $arr[$key]['unit_name'] = 'ounce';
        } else if($arr[$key]['unit_name'] === 'cup'){
            $arr[$key]['amount'] = (new UnitConverter())->convertAmount($arr[$key]['amount'], 'cup', 'tablespoon');
            $arr[$key]['unit_name'] = 'tablespoon';
        }
    }

    return $arr;
}


function generateBreakfast(){
    $array = $var = array(
        0 => array( 'amount' => 1, 'ingredient_name' => 'almond flour', 'unit_name' => 'cup' ),
        1 => array( 'amount' => ½, 'ingredient_name' => 'erythritol', 'unit_name' => 'cup' ),
        2 => array( 'amount' => 1, 'ingredient_name' => 'egg', 'unit_name' => 'large' )
    );

    $converted_ingredients = convertIngredients($array);

    return $converted_ingredients;
}

echo '<pre>';
print_r(generateBreakfast());
echo '</pre>';

So in the convertIngredients, we're calling the convertAmount method, but for some reason there.. it's passing this '1½' instead of just calling the method individually with each iteration.

If you take a look here: https://eval.in/944452 , the amount in erythritol is showing 16, but it should be 8 because 1/2 of 16 = 8.

  • 写回答

2条回答 默认 最新

  • duangejian6657 2018-01-30 00:28
    关注

    Replace vulgar_to_float with...

    public $vulgar_to_float = array('½' => 0.5);
    

    ... and it should work.

    As it stands, 16 * '1/2' expression is evaluated by the code. You probably hope that PHP automatically 'resolves' the second operand to a correct numeric value. But it doesn't, as the parsing rules (applied to a string in attempt to cast it to a number) don't treat '/' character in any special way - hence it's discarded, along with the rest of string.

    That means, value of 16 * '1/2' is essentially equal to 16 * 1 - which is, as you clearly noticed, 16.

    The change prevents this error - and makes life for PHP a little bit easier: if you're going to get a float as replaceUnicode return value, use floats in your map right from the start so that the engine won't have to spend time on type juggling.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题