PHP - Array_map中变量的字符串:Lambda函数

时间:2015-06-10 17:36:42

标签: php arrays string lambda

我目前正在为一个新项目学习PHP ....这个项目应该是所有可配置的,因此决定使用字符串作为配置的数据类型。我创建了以下MVP:

function decodifyAppLauncher($strAppLauncher)
    {
        $strAppLauncher= "BTN_DEPOSIT|BTN_DISPENSE|BTN_CHANGE";
        $BTN_DEPOSIT = "MSG78|ICONDEPOSIT|DEPOSIT";
        $BTN_DISPENSE = "MSG78|ICONDEPOSIT|DEPOSIT";
        $BTN_CHANGE = "MSG78|ICONDEPOSIT|DEPOSIT";
        $MSG78 = "ESTE ES UN MENSAJE";
        $ICONDEPOSIT = "ESTE ES UNA IMAGEN";
        $DEPOSIT = "ESTE ES UN PATH";
        $vMenu = array();
        $vElementsDecodified = array();


        // Read from AppLauncher and configure Menu
        $vAppLauncher = explode("|",$strAppLauncher);
        foreach ($vAppLauncher as $key => $strCurrentItem){ 
            // Decodify button information, MSG, ICON, PATH1 ... PATHN (CHANGES STRINGS TO VARIABLES)

            $vValues = array_map(function($a){ return $$a; }, explode("|", $$strCurrentItem));
            // Append our list-values to our menu-list, but with values decodified
            $vMenu[] = $vValues;
        }
            return $vMenu;
    }

这会产生以下错误:

Notice: Undefined variable: MSG78 in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21

Notice: Undefined variable: ICONDEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21

Notice: Undefined variable: DEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21

Notice: Undefined variable: MSG78 in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21

Notice: Undefined variable: ICONDEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21

Notice: Undefined variable: DEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21

Notice: Undefined variable: MSG78 in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21

Notice: Undefined variable: ICONDEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21

Notice: Undefined variable: DEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21

我知道我正在搞乱将字符串转换为变量名称,这个问题的最佳方法是什么?

我该怎么做array_map?

感谢!!!

EDIT1:

就我个人而言,我认为这不是一个重复的问题......我相信@dblue是对的我在这行中搞乱了变量范围:

  $vValues = array_map(function($a){ return $$a; }, explode("|", $$strCurrentItem));

我应该改为:

  $vValues = array_map(function($a) use ($$a) { return $$a; }, explode("|", $$strCurrentItem));

或者使用常量或全局变量,我现在无法测试它,但我会很快测试它。这个问题更多的是关于PHP中的lambda函数。

0 个答案:

没有答案