启用调用函数以返回另一个值

时间:2014-12-12 18:43:20

标签: php arrays

为什么这段代码不能给我正确的文字?

// My function
function getActivitiesDatas($datas, $got, $to_find) {
    foreach ($datas as $d) {
        if (array_search($got, $d)) {
            if (in_array($to_find, array_keys($d))) {
                return trim($d[$to_find]);
            }
        }
    }
}

// My array
$activitiesList = array(
    array(
        'dbTable'         =>   "Outfitters", 
        'dbPrefix'        =>   "OUT",
        'fullName'        =>   "F_ACTIVITY0001",
        'rewriteName'     =>   $R_ACTIVITY0001,
        'sectionType'     =>   "accommodation",
        'activeSeasons'   =>   "all",
        'weatherDep'      =>   "no"
    )
);

// My function call
$R_ACTIVITY0001 = "outfitters";
echo getActivitiesDatas($activitiesList, "OUT", "rewriteName");

我的问题如下:当我尝试从我的数组中返回$rewriteName参数时,当我调用我的函数时rewriteName为空。

当我在我的数组中尝试将值$R_ACTIVITY0001替换为"R_ACTIVITY0001"时,它会起作用。

为什么?

感谢。

2 个答案:

答案 0 :(得分:0)

问题是订单,你需要在初始化数组之前初始化$ R_ACTIVITY0001,如下所示:

$R_ACTIVITY0001 = "outfitters";

$activitiesList = array(
    array(
        'dbTable'         =>   "Outfitters", 
        'dbPrefix'        =>   "OUT",
        'fullName'        =>   "F_ACTIVITY0001",
        'rewriteName'     =>   $R_ACTIVITY0001,
        'sectionType'     =>   "accommodation",
        'activeSeasons'   =>   "all",
        'weatherDep'      =>   "no"
    )
);

答案 1 :(得分:0)

$R_ACTIVITY0001 = "outfitters";

$activitiesList = array(
    array(
        'dbTable'         =>   "Outfitters", 
        'dbPrefix'        =>   "OUT",
        'fullName'        =>   "F_ACTIVITY0001",
        'rewriteName'     =>   $R_ACTIVITY0001,
        'sectionType'     =>   "accommodation",
        'activeSeasons'   =>   "all",
        'weatherDep'      =>   "no"
    )
);

您的变量未在数组

之前初始化