在每个数组的末尾需要一个分号

时间:2012-07-23 04:50:31

标签: php arrays

当我将一个数组插入一个新的php文件时,我没有在每个数组的末尾得到一个分号。我需要在每个数组的末尾加一个分号。

File1中

  <?php
include_once("../dc/dcCommonLib.php");
include_once("../dc/persistence/UserData.php");
require_once("../../lib/include/connect.inc.php");

$_SESSION["survey"] = "BestBuyAug2012";
$idLink = dbconnect($_SESSION["survey"]);
$surveyWidget = rendererSurveyLoadHandler();
setDataToSession("surveywidget",$surveyWidget);
$userData = &UserData::getInstance();
$userData->setSurvey($surveyWidget);
saveFkidsToOidToFkidMappingArray($userData->OidToFkidMapping);

// prepare demo array
prepareItemArray($userData->OidToFkidMapping["Demographic"]["Q"],$demo,"b");

// prepare item array
prepareItemArray($userData->OidToFkidMapping["Default"]["Q"],$item,"i");
prepareItemArray($userData->OidToFkidMapping["Default"]["M"],$item,"i");

// prepare comment array
prepareCommentArray($userData->OidToFkidMapping["Comment"]["C"],$comment);


if(!function_exists('file_put_contents')) 
{
    function file_put_contents($filename, $data,$type, $file_append = false)
     {
              $fp = fopen($filename, (!$file_append ? 'w+' : 'a+'));
         if(!$fp)
             {
                     trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
                     return;
             }

                if($type)//code which is concerned with the execution of array
            {

                    $count = count($data);
                    $i = 1;
                    //loop through your type array and append comma and double qoutes , 
                    foreach($data as $key=>$val)
                 {

                    if($i = $count)

                        $data[$key] = '"'.$data[$key].'",';
                        $i++;
                }

             //run same preg_replace as before
             $data2 = preg_replace('/[\[\]]/','"',print_r($data,TRUE));

                //write to file 
             fputs($fp, "$" . $type . " = " . print_r($data2,TRUE));  

             }
                 else 
            {
                    fputs($fp, print_r($data, TRUE));

                 }
                 fclose($fp);
         }


}
print("Strart writting ...................");
file_put_contents("staticSurveyDataFileTest.php", $file,"",true);
file_put_contents("staticSurveyDataFileTest.php", "<?php\n","",true);
file_put_contents("staticSurveyDataFileTest.php", $item,"itemFkids",true);
file_put_contents("staticSurveyDataFileTest.php", $demo,"demoFkids",true);
file_put_contents("staticSurveyDataFileTest.php", $comment,"commentFkids",true);
file_put_contents("staticSurveyDataFileTest.php", "?>","",true);

print("written ...");

function prepareItemArray($userDataArray, &$item, $itemStr) {
    if(is_array($userDataArray)) {
        foreach($userDataArray as $questId=>$respGrp) {
            $selectionGrp=array_unique($respGrp);
            if(count($selectionGrp)==1) {
                $respGrpId=key($respGrp);
                $respId = key($respGrp[$respGrpId]);
                $item[$questId."_".key($respGrp)]=$itemStr.$respGrp[$respGrpId][$respId];           
            }else {
                foreach ($respGrp as $respGrpId=>$resp) {
                    $respGrp[$respGrpId]=$itemStr.$resp;
                }
                $item[$questId."_".key($respGrp)]=$respGrp;
            }
        }
    }   
}

function prepareCommentArray($userDataArray, &$item) {
    if(is_array($userDataArray)) {
        foreach($userDataArray as $questId=>$respGrp) {
            $selectionGrp=array_unique($respGrp);
            if(count($selectionGrp)==1) {
                $respGrpId=key($respGrp);               
                $item[$questId."_".key($respGrp)]=$respGrp[$respGrpId]["fk_id"];            
            }
        }
    }   
}

?>

我在File2中得到的输出是(如前所述,我需要在每个数组的末尾加一个分号)

<?php
$itemFkids = Array
(
    "203_19" => "i27",
    "207_22" => "i28",
    "357_22" => "i99",
    "370_70" => "i104",
    "377_72" => "i105",
    "140_8" => "i1",
    "141_8" => "i2",
    "142_8" => "i3",
    "143_8" => "i4",
    "144_8" => "i5",

)//need a semicolon here
$demoFkids = Array
(
    "129_2" => "b1",
)//need a semicolon here
$commentFkids = Array
(
    "373_375" => "1",
    "380_382" => "2",
)//need a semicolon here
?>

1 个答案:

答案 0 :(得分:1)

我之前没有这样做,但你不能再添加另一对file_put_contents吗?结果怎么样?

file_put_contents("staticSurveyDataFileTest.php", $file,"",true);
file_put_contents("staticSurveyDataFileTest.php", "<?php\n","",true);
file_put_contents("staticSurveyDataFileTest.php", $item,"itemFkids",true);
file_put_contents("staticSurveyDataFileTest.php", ";","",true);
file_put_contents("staticSurveyDataFileTest.php", $demo,"demoFkids",true);
file_put_contents("staticSurveyDataFileTest.php", ";","",true);
file_put_contents("staticSurveyDataFileTest.php", $comment,"commentFkids",true);
file_put_contents("staticSurveyDataFileTest.php", ";\n?>","",true);