将代码转换为函数php

时间:2013-07-14 08:12:52

标签: php arrays function

好的,所以我有以下代码(完全按照我想要的数组工作) 它适用于名为courseInfo()的数组并对其进行更改。

$sizev1 = sizeof($courseInfo);
for ($row = 0; $row < $sizev1; $row++)
{
$tempsemhrs= $courseInfo[$row][3];
$tempday= $courseInfo[$row][4];
    if ($tempday == "א") $tempday=1;
    if ($tempday == "ב") $tempday=2;
    if ($tempday == "ג") $tempday=3;
    if ($tempday == "ד") $tempday=4;
    if ($tempday == "ה") $tempday=5;
    if ($tempday == "ו") $tempday=6;
$tempstarttime= $courseInfo[$row][5];
    if( (StrDelete($tempstarttime, 2, 3)-7) <10 )
        if( (StrDelete($tempstarttime, 0, 3)) == "45" )
            $tempstarttime= "0".(StrDelete($tempstarttime, 2, 3)-7+1);
        else
            $tempstarttime= "0".(StrDelete($tempstarttime, 2, 3)-7);
    if( (StrDelete($tempstarttime, 2, 3)-7) >=10 )
        if( (StrDelete($tempstarttime, 0, 3)) == "45" )
            $tempstarttime= (StrDelete($tempstarttime, 2, 3)-7+1);
        else
            $tempstarttime= (StrDelete($tempstarttime, 2, 3)-7);        


$courseInfo[$row][6]= ( $courseInfo[$row][2].$tempsemhrs.$tempday.$tempstarttime);

}

现在,还有一些地方我可能不得不使用该代码所以我想把它变成一个函数而不是加倍代码,但是当我尝试它不再工作时,我所做的只是将它复制到函数{}并改变了里面的名字,它不起作用,我也把它复制到这里,我喜欢有人告诉我哪里出错了,为什么它现在不起作用?

function addSortingCode ($tableChange)
{
$sizev1 = sizeof($tableChange);
for ($row = 0; $row < $sizev1; $row++)
    $tempsemhrs= $tableChange[$row][3];
    $tempday= $tableChange[$row][4];
        if ($tempday == "א") $tempday=1;
        if ($tempday == "ב") $tempday=2;
        if ($tempday == "ג") $tempday=3;
        if ($tempday == "ד") $tempday=4;
        if ($tempday == "ה") $tempday=5;
        if ($tempday == "ו") $tempday=6;
    $tempstarttime= $tableChange[$row][5];
        if( (StrDelete($tempstarttime, 2, 3)-7) <10 )
            if( (StrDelete($tempstarttime, 0, 3)) == "45" )
                $tempstarttime= "0".(StrDelete($tempstarttime, 2, 3)-7+1);
            else
                $tempstarttime= "0".(StrDelete($tempstarttime, 2, 3)-7);
        if( (StrDelete($tempstarttime, 2, 3)-7) >=10 )
            if( (StrDelete($tempstarttime, 0, 3)) == "45" )
                $tempstarttime= (StrDelete($tempstarttime, 2, 3)-7+1);
            else
                $tempstarttime= (StrDelete($tempstarttime, 2, 3)-7);        


    $tableChange[$row][6]= ( $tableChange[$row][2].$tempsemhrs.$tempday.$tempstarttime);
    }
 }
addSortingCode ($courseInfo);

2 个答案:

答案 0 :(得分:2)

你可以这样做:

//return the final contructed array(here $tableChange) as function output
function addSortingCode($tableChange)
{

     //your code
     return $tableChange;
}

//calling of the function

 $output = addSortingCode($courseInfo);

答案 1 :(得分:0)

在函数内不会修改$tableChange变量。尝试通过引用传递将函数签名更改为

function addSortingCode (&$tableChange)