在$ val字段中需要双引号

时间:2012-07-20 06:52:01

标签: php arrays

我们正在创建一个文件,以便在执行文件时将数组推送到新的PHP文件中。 我们能够在新的PHP文件中打印数组细节,但是我们无法获得$ val字段的双引号。

代码如下

<?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')) {
    /* ... define that function then ... */ 
}

print("Strart writting ...................");
file_put_contents("Test.php", $file,"",true);
file_put_contents("Test.php", "<?php\n","",true);
file_put_contents("Test.php", $item,"itemFkids",true);
file_put_contents("Test.php", $demo,"demoFkids",true);
file_put_contents("Test.php", $comment,"commentFkids",true);
file_put_contents("Test.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"];            
            }
        }
    }   
}

?>

我们在Test.php中获得的数组是

<?php
$itemFkids = Array
(
    "203_19" => i27,//need double qoutes for all the $val fields
    "207_22" => i28,//need double qoutes for all the $val fields
    "357_22" => i99,//need double qoutes for all the $val fields
    "370_70" => i104,//need double qoutes for all the $val fields
    "377_72" => i105,//need double qoutes for all the $val fields
)
$demoFkids = Array
(
    "129_2" => b1//need double qoutes for all the $val fields
)
$commentFkids = Array
(
    "373_375" => 1,//need double qoutes for all the $val fields
    "380_382" => 2//need double qoutes for all the $val fields
)
?>

感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

  

var_export

     

var_export()获取有关给定变量的结构化信息。它类似于var_dump(),但有一个例外: 返回的表示形式是有效的PHP代码。

     

http://php.net/var_export

(突出我的。)

答案 1 :(得分:2)

$data[$key] .= ',';替换为$data[$key] .= '",';

编辑:

$data[$key] = '"'.$data[$key].'",';

答案 2 :(得分:2)

不确定您要求的是什么,但据我所知,您希望在数组值周围添加双引号。

这样做的唯一方法是在代码中添加它们作为值的一部分。以下是一些您可以尝试自己的测试用例:

<?php 
    $value_3 = 'value_3';

    $data = array( 'key_1' => 'value_1' // value without quotes
                  ,'key_2' => '"value_2"' // value with quotes
                  ,'key_3' => '"'.$value_3.'"' // adding quotes to value
    );

    echo '<pre>'.print_r($data, true).'</pre>';

如果你不想乱用现有的代码,你最后也可以循环遍历数组并添加双引号:

<?php 
    foreach( $data AS $key => $value ){
        $data[$key] = '"'.trim( $value, '"').'"'; // add double quotes to all values
    }

    echo '<pre>'.print_r($data, true).'</pre>';