PHP - 你能帮助稍微改变这个功能吗?

时间:2010-03-24 05:00:59

标签: php

我有以下功能,我根据别人的功能进行了修改:

function showCombinations($string, $nameString, $linkParts, $i){
    $wordDivider = "/"; //the divider between the words/values

    if ($i >= count($linkParts)){
        echo "<a href='".trim($string)."'>".trim($nameString)."</a>, ";
    } else {
        foreach ($linkParts[$i] as $currentTrait){

            if ($currentTrait['name']=="urltext"){
                $currentNameStringName=""; //ignore
            } else {
                $currentNameStringName=$currentTrait['name'];
            }

            if ($nameString!=""){
                $currentNameString=$nameString." - ".$currentNameStringName;
            } else {
                $currentNameString=$nameString.$currentNameStringName;
            }
            showCombinations($string.$currentTrait['value'].$wordDivider, $currentNameString, $linkParts, $i + 1);
        }


    }
}
showCombinations('', '', $linkParts, 0);

我需要改变这一点,而不是ECHO,我希望它能够构建组合,所以我可以这样做:

$result = showCombinations('', '', $linkParts, 0);
echo $result;

我需要这样,因为我必须修改$result,而不仅仅是回复它。

1 个答案:

答案 0 :(得分:2)

function showCombinations($string, $nameString, $linkParts, $i){
    $wordDivider = "/"; //the divider between the words/values
    $result = '';

    if ($i >= count($linkParts)){
        $result .= "<a href='".trim($string)."'>".trim($nameString)."</a>, ";
    } else {
        foreach ($linkParts[$i] as $currentTrait){

            if ($currentTrait['name']=="urltext"){
                $currentNameStringName=""; //ignore
            } else {
                $currentNameStringName=$currentTrait['name'];
            }

            if ($nameString!=""){
                $currentNameString=$nameString." - ".$currentNameStringName;
            } else {
                $currentNameString=$nameString.$currentNameStringName;
            }
            $result .= showCombinations($string.$currentTrait['value'].$wordDivider, $currentNameString, $linkParts, $i + 1);
        }


    }

    return $result;
}

$result = showCombinations('', '', $linkParts, 0);
echo $result;