duanmu5641 2017-04-17 13:50
浏览 218
已采纳

PHP - 在foreach中使用时功能不起作用

I'm doing a loop thro XML and collecting data from it. Some values need to be translated so i've made a switch function, which is wrapped inside another function and called on every loop to check the original value and change it according defined new value within the switch.

The problem is if i include the switch inside the foreach the original value get's changed properly (strings of the original variable $foo are translated). But if i wrap the switch in function and call that function within the loop then the original value doesn't get changed. It needs to be if ($foo == 'one') $foo == 'ein'... Here's my code:

function checkTranslation($foo) {
    switch($foo) {
        case('one'): $foo = 'ein'; break;
        case('two'): $foo = 'zwei'; break;
        ...
    }
};

foreach ($XML -> some -> data as $item) {
    $foo = $item['value'];
    checkTranslation($foo);
}
  • 写回答

1条回答 默认 最新

  • dongya1228 2017-04-17 13:53
    关注

    Change your function signature to take the value by reference:

    function checkTranslation(&$foo) { /* ... */ }
    

    Why is that?

    When you pass something into a function, the value (a copy, so to speak) of that variable will be passed in. Whatever you do to it inside the function will not reflect to the original variable outside of that function.

    However, if you add & in front of a parameter in the function signature, PHP will actually pass it in by reference, which means the function will operate on the very variable you passed in. Hence, any changes made within the function will be reflected outside, even once the function finishes.

    If you are struggling with this concept, it can help to choose different names for the variable you pass in and the one in the method signature. This helped me with this issue when I started out:

    function checkTranslation(&$input) { /* ... */ } // method
    checkTranslation($foo);                          // using the method
    

    Alternatively...

    Just return $foo from your method, like so:

    function checkTranslation($foo) {
        /* ... */
        return $foo;
    }
    

    And then, outside, something like:

    $foo = checkTranslation($item['value'];);
    

    PS 1

    I'd recommend renaming the function so that is becomes more obvious what it does. checkTranslation() sounds like it would simply check if the input is valid and return a boolean or some other indicative value. However, you are actually (trying to) manipulate the input, so maybe something like translate() would be a better fit?

    PS 2

    Also note what EatPeanutButter pointed out in his comment: currently, you are overwriting $foo with every iteration of your loop. That doesn't seem to make a lot of sense.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度