0

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

 $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>

如果它放入数组中,整个东西都会死掉

我确定我只是累了,可能会想出其他借口,但简而言之,我只是不知道我做错了什么。

4

5 回答 5

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>
于 2013-01-11T18:00:34.960 回答
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>
于 2013-01-11T18:03:51.650 回答
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"];
});

小提琴

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

于 2013-01-11T18:04:41.637 回答
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"];
}
于 2013-01-11T18:10:04.270 回答
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>
于 2013-01-11T18:10:37.547 回答