Javascript Json:如何重写此数组以特定格式输出

时间:2013-01-11 17:58:13

标签: javascript php arrays json

我正在使用以下内容来收集我需要使用的字段数组:

 $reqFlds = array();
    $results = mysql_query("aid, fld_name from field
where aid = $acct_id2 and req_flag = 1");
        while($result = mysql_fetch_assoc($results))
            {
            $reqFlds[] = $result;
            }

    <script language="javascript">
        var reqFlds= <?php echo "new Array(". trim(json_encode($reqFlds), '[]').")"; ?>;
    </script>

我的“reqFlds”数组吐出如下:

<script language="javascript">
    var reqFlds = new Array(
    {"aid":"15","fld_name":"Job Issued"},
    {"aid":"18","fld_name":"Job Title"},
    {"aid":"20","fld_name":"Banner Details"},
    {"aid":"202","fld_name":"Flyer Details"},
    {"aid":"2134","fld_name":"Code Not above"},
    {"aid":"5862","fld_name":"Quantity"});

</script>

我得到了一个需要像这样的数组数据的函数:

var fldToUse = ['15||Job Issued', '18||Job Title', '20||Banner Details'];

如何重写我的数组[reqFlds],使其以上述格式吐出:

提前谢谢。

最近的修改 - 以下是我放置代码和结果的确切方法

<?php
$reqFlds  = array();
$results = mysql_query("select id, display_name from field where account_id = $acct_id2 and required_flag = 1");
    while($RequireResult = mysql_fetch_assoc($results))
        {
            $reqFlds[] = $RequireResult;
        }

foreach($reqFlds as $reqFlds)
    {
    $reqFlds = $reqFlds["id"] . "||" . $reqFlds["display_name"];
}
?>

<script language="javascript">
    var requiredFieldsPRS = <?php echo "new Array(". trim(json_encode($reqFlds), '[]').")"; ?>;
    var holdReqFlds = <?php echo json_encode($reqFlds); ?>;
</script>

如果将这样的代码留在循环外的foreach中,我只能在查看源代码时得到一个 - 这是如此接近因为是按照我需要的方式布局,但这只是数组中的第一个值

<script language="javascript">
    var requiredFieldsPRS = new Array("'5862||Quantity'");
    var holdReqFlds = "'5862||Quantity'";
</script>

如果它放入数组,整个事情就会死掉

我确定我只是累了,可能会想出其他的借口,但简短的说法就是我不 我做错了什么。

5 个答案:

答案 0 :(得分:1)

var newArray = [];

for(var i = 0; i != reqFlds.length; ++i)
{
  newArray.push(reqFlds[i].aid + "||" + reqFlds[i].fld_name);
}

newArray现在将采用您需要的格式。这是一个客户端解决方案。如果您需要在PHP方面进行更改,那么这是一个不同的解决方案:

$reqFlds = array();
    $results = mysql_query("aid, fld_name from field
where aid = $acct_id2 and req_flag = 1");
        while($result = mysql_fetch_assoc($results))
            {
            $reqFlds[] = $result['aid'] . '||' . $result['fld_name'];
            }

    <script language="javascript">
        var reqFlds= <?php echo "new Array(". trim(json_encode($reqFlds), '[]').")"; ?>;
    </script>

答案 1 :(得分:0)

这应该可以解决问题。很干净。

<?php 
    $reqFlds = array();
    $results = mysql_query("aid, fld_name from field where aid = $acct_id2 and req_flag = 1");

    while ($result = mysql_fetch_assoc($results)) {
        $reqFlds[] = $result['aid'] . '||' . $result['fld_name'];
    }
?>

<script language="javascript">
    var reqFlds= <?php echo json_encode($reqFlds); ?>;
</script>

答案 2 :(得分:0)

在客户端,您可以尝试:

  var reqFlds = new Array(
    {"aid":"15","fld_name":"Job Issued"},
    {"aid":"18","fld_name":"Job Title"},
    {"aid":"20","fld_name":"Banner Details"},
    {"aid":"202","fld_name":"Flyer Details"},
    {"aid":"2134","fld_name":"Code Not above"},
    {"aid":"5862","fld_name":"Quantity"});


var transformed = reqFlds.map(function(item) {
  return item["aid"] + "||" + item["fld_name"];
});

fiddle

对于服务器端,Eli的解决方案看起来是正确的。

答案 3 :(得分:0)

这应该可以解决问题

var reqFldsFormatted = new Array();

for (var i=0; i<reqFlds.length; i++) {
    // Written for readability, not execution speed
    reqFldsFormatted[i] = reqFlds[i]["aid"] + "||" + reqFlds[i]["fld_name"];
}

答案 4 :(得分:0)

您可以在PHP代码中转换数组,如下所示:

$reqFlds = array();
$results = mysql_query("select aid, fld_name from field where aid = $acct_id2 and req_flag = 1");
while ($result = mysql_fetch_assoc($results)) {
    $reqFlds[] = $result;
}
// at this point the array becomes something like this
//
// array(
//     array("aid" => "15","fld_name" => "Job Issued"),
//     array("aid" => "18","fld_name" => "Job Title"),
//     array("aid" => "20","fld_name" => "Banner Details"),
//     array("aid" => "202","fld_name" => "Flyer Details"),
//     array("aid" => "2134","fld_name" => "Code Not above"),
//     array("aid" => "5862","fld_name" => "Quantity")
// );
//
// here is how you transform array elements to desired format:
foreach($reqFlds as &$reqFld) {
    $reqFld = $reqFld["aid"] . "||" . $reqFld["fld_name"];
}
unset($reqFld);

然后你可以做:

var reqFlds = <?php echo json_encode($reqFlds); ?>;

输出:

<script>
var reqFlds = ["15||Job Issued","18||Job Title","20||Banner Details","202||Flyer Details","2134||Code Not above","5862||Quantity"];
</script>