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.

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

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛