Insertion of an array into a new php file
<p>I am trying to insert an array into a php file . However I am able to insert the array into a php file but the format is not as desired . We have 2 files</p>

<p>File1.php</p>

<pre><code>&lt;?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 = &amp;UserData::getInstance();
$userData-&gt;setSurvey($surveyWidget);
saveFkidsToOidToFkidMappingArray($userData-&gt;OidToFkidMapping);

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

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

// prepare comment array
prepareCommentArray($userData-&gt;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=&gt;$val) {

     //if we are before last key, append comma    
     if($i &lt; $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", "&lt;?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", "?&gt;","",true);
print("written ...");

function prepareItemArray($userDataArray, &amp;$item, $itemStr) {
    if(is_array($userDataArray)) {
        foreach($userDataArray as $questId=&gt;$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=&gt;$resp) {
                    $respGrp[$respGrpId]=$itemStr.$resp;
                }
                $item[$questId."_".key($respGrp)]=$respGrp;
            }
        }
    }   
}

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

?&gt;
</code></pre>

<p>The array from this file is inserted into another file by name staticSurveyDataFileTest.php</p>

<p>staticSurveyDataFileTest.php</p>

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

<p>We need to insert a file something like this</p>

<p>Desired Output</p>

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

        .
        .
.

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

<p>I did try to search in google and other php websites but did not find much about this . 
So I would really appreciate IF YOU CAN EDIT MY CODE and make this work .
Thank you in advance :) </p>
