-2

配列を新しい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
?>
4

1 に答える 1

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);
于 2012-07-23T05:00:54.407 に答える