Arrays into new PHP file
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/11502159/inserting-array-into-a-php-file">Inserting array into a PHP file</a>  </p>
</blockquote>



<p>We are trying to insert an array into a php file . However we are able to insert the array into a php file but the format is not as desired . We have 2 files .</p>

<pre><code>File1.php

    &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 staticSurveyDataFileTest.php</p>

<pre><code>staticSurveyDataFileTest.php

    &lt;?php
    $itemFkids = Array
    (
        "203_19" =&gt; i27,//double quotes missing for all the lines . See below for desired output//
        "207_22" =&gt; i28,
        "357_22" =&gt; i99,
        "370_70" =&gt; i104,
        "377_72" =&gt; i105,
        "140_8" =&gt; i1,
        "141_8" =&gt; i2,
        "142_8" =&gt; i3,
        "143_8" =&gt; i4,
        "144_8" =&gt; i5,
        "145_8" =&gt; i6,
        "151_8" =&gt; i7,
        "152_8" =&gt; i8,
        "158_8" =&gt; i9,
        "164_8" =&gt; i10,
        "165_8" =&gt; i11,
        "166_8" =&gt; i12,
        "167_8" =&gt; i13,
        "168_8" =&gt; i14,
        "169_8" =&gt; i15,
        "175_8" =&gt; i16,
        "176_8" =&gt; i17,
        "182_8" =&gt; i18,
        "183_8" =&gt; i19,
        "184_8" =&gt; i20,
        "190_8" =&gt; i21,
        "196_8" =&gt; i22,
        "197_8" =&gt; i23,
        "198_8" =&gt; i24,
        "199_8" =&gt; i25,
        "200_8" =&gt; i26,
        "214_8" =&gt; i29,
        "215_8" =&gt; i30,
        "216_8" =&gt; i31,
        "222_8" =&gt; i32,
        "223_8" =&gt; i33,
        "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
    )//comma missing
    $commentFkids = Array
    (
        "373_375" =&gt; 1,
        "380_382" =&gt; 2
    )//comma missing
    ?&gt;

We need to insert a file something like this

Desired Output

    &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>Please help me by editing or helping me edit the same code 
Thanks for your help in advance :)</p>
