2012-07-17 35 views
1

我想插入一個數組到一個PHP文件。不過,我可以將數組插入到php文件中,但格式不符合要求。我們有2個文件插入一個數組到一個新的PHP文件

File1.php

<?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){ 

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

    //if we are before last key, append comma  
    if($i < $count) 
      $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", "<?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"];    
      } 
     } 
    } 
} 

?> 

從這個文件中的陣列插入到另一個文件的名字staticSurveyDataFileTest.php

staticSurveyDataFileTest.php

<?php 
$itemFkids = Array 
(
    "203_19" => i27,//double quotes missing 
    "207_22" => i28,//double quotes missing 
    "357_22" => i99,//double quotes missing 
    "370_70" => i104,//double quotes missing 
    "377_72" => i105,//double quotes missing 
    "140_8" => i1,//double quotes missing 
    "141_8" => i2,//double quotes missing 
    "142_8" => i3,//double quotes missing 
    "143_8" => i4,//double quotes missing 
    "144_8" => i5,//double quotes missing 
    "145_8" => i6,//double quotes missing 
    "151_8" => i7,//double quotes missing 
    "152_8" => i8,//double quotes missing 
    "158_8" => i9,//double quotes missing 
    "164_8" => i10,//double quotes missing 
    "165_8" => i11,//double quotes missing 
    "166_8" => i12,//double quotes missing 
    "167_8" => i13,//double quotes missing 
    "168_8" => i14,//double quotes missing 
    "169_8" => i15,//double quotes missing 
    "175_8" => i16,//double quotes missing 
    "176_8" => i17,//double quotes missing 
    "182_8" => i18,//double quotes missing 
    "183_8" => i19,//double quotes missing 
    "184_8" => i20,//double quotes missing 
    "190_8" => i21,//double quotes missing 
    "196_8" => i22,//double quotes missing 
    "197_8" => i23,//double quotes missing 
    "198_8" => i24,//double quotes missing 
    "199_8" => i25,//double quotes missing 
    "200_8" => i26,//double quotes missing 
    "214_8" => i29,//double quotes missing 
    "215_8" => i30,//double quotes missing 
    "216_8" => i31,//double quotes missing 
    "222_8" => i32,//double quotes missing 
    "223_8" => i33,//double quotes missing 
    "224_8" => i34, 
    "225_8" => i35, 
    "226_8" => i36, 
    "227_8" => i37, 
    "233_8" => i38, 
    "234_8" => i39, 
    "235_8" => i40, 
    "236_8" => i41, 
    "237_8" => i42, 
    "243_8" => i43, 
    "244_8" => i44, 
    "245_8" => i45, 
    "251_8" => i46, 
    "252_8" => i47, 
    "253_8" => i48, 
    "254_8" => i49, 
    "255_8" => i50, 
    "256_8" => i51, 
    "257_8" => i52, 
    "263_8" => i53, 
    "264_8" => i54, 
    "265_8" => i55, 
    "266_8" => i56, 
    "267_8" => i57, 
    "268_8" => i58, 
    "269_8" => i59, 
    "270_8" => i60, 
    "271_8" => i61, 
    "272_8" => i62, 
    "278_8" => i63, 
    "279_8" => i64, 
    "280_8" => i65, 
    "281_8" => i66, 
    "282_8" => i67, 
    "283_8" => i68, 
    "289_8" => i69, 
    "290_8" => i70, 
    "296_8" => i71, 
    "297_8" => i72, 
    "298_8" => i73, 
    "299_8" => i74, 
    "300_8" => i75, 
    "301_8" => i76, 
    "307_8" => i77, 
    "308_8" => i78, 
    "309_8" => i79, 
    "310_8" => i80, 
    "311_8" => i81, 
    "312_8" => i82, 
    "313_8" => i83, 
    "314_8" => i84, 
    "315_8" => i85, 
    "316_8" => i86, 
    "317_8" => i87, 
    "318_8" => i88, 
    "324_26" => i89, 
    "328_37" => i90, 
    "332_48" => i91, 
    "338_59" => i92, 
    "344_59" => i93, 
    "345_59" => i94, 
    "351_8" => i95, 
    "352_8" => i96, 
    "353_8" => i97, 
    "354_8" => i98, 
    "364_8" => i100, 
    "365_8" => i101, 
    "366_8" => i102, 
    "367_8" => i103 
)//comma missing 
$demoFkids = Array 
(
    "129_2" => b1//double quotes missing 
)//comma missing 
$commentFkids = Array 
(
    "373_375" => 1,//double quotes missing 
    "380_382" => 2//double quotes missing 
)//comma missing 
?> 

我們需要插入一個像這樣的文件

Desired Outp ut

<?php 
    $itemFkids = Array 
    (
     "203_19" => "i27",//notices the double quotes here 
     "207_22" => "i28", 
     "357_22" => "i99", 
     . 
     . 
     . 

     . 
     . 
. 

    ),//notice a comma here 
    $demoFkids = Array 
    (
     "129_2" => "b1"//notices the double quotes here 
    ),//notice a comma here 
    $commentFkids = Array 
    (
     "373_375" => "1", 
     "380_382" => "2"//notices the double quotes here 
    ),//notice a comma here 
    ?> 

我曾嘗試在谷歌和其他php網站上搜索,但沒有找到太多關於此。 所以我會很感激,如果你可以編輯我的代碼,並使這項工作。 預先感謝您:)

+0

鑑於數據的嚴格和有限格式,您可以通過在右花括號上進行字符串替換來輕鬆完成此操作。 – 2012-07-17 06:05:22

+0

@BillyMoon你好比利。你能告訴我一行代碼 – jat 2012-07-17 06:21:12

+0

你打算如何從你的文件中檢索數據?在你的任何一個例子中,它都不是有效的php,因爲每個聲明的數組都應該以分號';'結尾; – Crisp 2012-07-17 07:20:12

回答

0

您應該使用數據庫,在我看來。
無論如何,我認爲foreach將工作,如果沒有找到更好的解決方案。

實施例:

$second_array = array(); 

foreach($first_array as $key => $value){ 
    $second_array[$key]='&quot;'.$value.'&quot;'; 
} 

這假設引號是HTML顯示。

+0

嗨,歡迎來到SO。請嘗試以普通格式編寫答案,而不是所有等寬代碼。對於形成適當的句子還要多加小心一點,也是值得讚賞的。謝謝。 – deceze 2012-07-17 07:12:30

+0

10-4 ------------我刪除了我的......輪到你了。 – 2012-07-17 08:09:01

+0

@Lee Bishop我應該在哪一行添加代碼? – jat 2012-07-17 08:29:21

0

結賬serialisation。這是與你正在做的不同的方法,但更清潔。 簡單來說,這是一種獲取PHP對象的方法,將它轉換爲一個字符串(可以保存,讀取等),並且反序列化將會將其轉換回原來的樣子。

+0

我一定會閱讀它,但我需要使我的代碼儘快工作。那麼你可以編輯我的相同代碼並使其工作? – jat 2012-07-17 06:53:56

相關問題