【问题标题】:Get value of parse JSON from php array从 php 数组中获取解析 JSON 的值
【发布时间】:2014-10-15 22:00:12
【问题描述】:
$query = "SELECT Item_Name from item_details";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
    $optionValue[] = "<option value='" . $row['Item_Name'] . "'>" . $row['Item_Name'] . "</option>";
}

$query1 = "SELECT Item_Name from item_addon";
$result1 = mysql_query($query1);

while ($row = mysql_fetch_array($result1)) {
    $optionValue1[] = "<option value='" . $row['Item_Name'] . "'>" . $row['Item_Name'] . "</option>";
}

return json_encode(array("a" => $optionValue, "b" => $optionValue1));

我需要解析ab的数据

optionDetails = $.parseJSON(data);

for (i = 0; i < optionDetails.length; i++) {
    det_item_name = optionDetails[i];
    $('#det_item_name').append(det_item_name);
}

optionAdd = $.parseJSON(data)
for (i = 0; i < optionAdd.length; i++) {
    det_item_name = optionAdd[i];

    $('#add_name').append(add_name);
}

如何解析optionDetailsoptionAddab的数据?

【问题讨论】:

  • 这不正是您在解析数据时所做的吗?你登录data 看看它包含什么,我们不知道,因为你没有发布任何从 PHP 获取 JSON 的代码
  • 看起来您正在使用 jQuery,如果您正在对 php 文件的数据进行 AJAX 调用,请使用 $.getJSON,它应该会自动为您解析响应为 JSON,以便你应该可以使用类似data.adata.b

标签: javascript php mysql ajax json


【解决方案1】:

试试这个

var json_array = $.parseJSON(data);
var optionDetails = json_array.a;

for (i = 0; i < optionDetails.length; i++) {
    det_item_name = optionDetails[i];
    $('#det_item_name').append(det_item_name);


}

var optionAdd = json_array.b;
for (i = 0; i < optionAdd.length; i++) {
    det_item_name = optionAdd[i];

    $('#add_name').append(add_name);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多