Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:08:05+00:00 2026-06-07T16:08:05+00:00

I am trying to print an array into a file staticSurveyDataFileTest.php . I am

  • 0

I am trying to print an array into a file staticSurveyDataFileTest.php . I am able to print the array but not in the ideal format.

Take a look into the following code

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) {  
        fputs($fp, "$".$type."=".preg_replace("/[\[\]]/","\"",print_r($data, 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"];            
            }
        }
    }   
}

?>

The contents(input) which I get in the staticSurveyDataFileTest.php is

<?php
$itemFkids=Array
(
    "203_19" => i27 //Notice that the Comma's(,) are not present 
    "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
    "145_8" => i6
    "151_8" => i7
    "152_8" => i8
    "158_8" => i9
    "164_8" => i10
    "165_8" => i11
    "166_8" => i12
    "167_8" => i13
    "168_8" => i14
    "169_8" => i15
    "175_8" => i16
    "176_8" => i17
    "182_8" => i18
    "183_8" => i19
    "184_8" => i20
    "190_8" => i21
    "196_8" => i22
    "197_8" => i23
    "198_8" => i24
    "199_8" => i25
    "200_8" => i26
    "214_8" => i29
    "215_8" => i30
    "216_8" => i31
    "222_8" => i32
    "223_8" => i33
    "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
)
$demoFkids=Array
(
    "129_2" => b1
)
$commentFkids=Array
(
    "373_375" => 1
    "380_382" => 2
)
?>

Ideally I want to something like this

<?php
$itemFkids=Array
(
    "203_19" => i27, //notice the comma's present here
    "207_22" => i28,
    "357_22" => i99,
    "370_70" => i104,
    "377_72" => i105,
    "140_8" => i1,
.
.
.
.
.

..
What are the changes that I need to do in the code ?
Please Help .
Thanks in advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:08:06+00:00Added an answer on June 7, 2026 at 4:08 pm

    My solution requires an extra preg_replace, but it should get the job done. I am assuming that all of your values in your array come in the format “i[somenumbers]”. If not, you need a different regex.

    if($type) 
        fputs($fp, "$".$type."=".preg_replace("/[\[\]]/","\"",print_r($data, TRUE)));
    

    Changes to:

    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));     
    
     }
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert an array into a php file . However I
Hi I'm trying to convert the XML file into associative array by using the
I am trying to decode a JSON string into an array but i get
I have an ajax call to a php file that encodes the array into
I am trying to decode some JSON into a php array. Here's the code
I am trying to download a .csv file into an array and then parse
I'm trying to read a text file containing 20 names into an array of
I am trying to output mysql query results into JSON format,but struggling to output
I'm trying to insert into an array at a certain point: $hi = test;
I am trying to print an int[] array from a seperate method in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.