这个2-D关联数组有什么问题?

时间:2013-08-27 16:52:12

标签: php javascript multidimensional-array associative-array

我正在尝试在Javascript中创建一个二维关联数组(第一个索引数字,第二个索引关联)。我正在设计的页面将包含多个字段,以及一个允许用户选择不同“更新日期”的下拉菜单,这些字段将使用该更新的值重新填充。我正在使用php生成javascript代码:

这是php代码的样子(我将其格式化以便于阅读):

<script type='text/javascript'>
<?php
$i = 0;
echo "var updates = new Array();";
foreach ($risk_data['updates'] as $update) {
    echo "updates[" . $i . "] = new Array();";
    foreach ($update as $key => $value) {
        echo "updates[" . $i . "]['" . $key . "'] = '" . $value . "';";
    }
    $i++;
}
?>

function update_fields() {

    var update_index = document.getElementById('select_update').selectedIndex;
    alert(update_index);
    document.getElementById('impact').innerHTML = updates[update_index]['impact'];
    document.getElementById('probability').innerHTML = updates[update_index]['probability'] + '%';
    document.getElementById('impact_effect').innerHTML = updates[update_index]['impact_effect'];
    document.getElementById('cost_impact').innerHTML = '$' + updates[update_index]['cost_impact'];
    document.getElementById('overall_impact').innerHTML = updates[update_index]['overall_impact'];
    document.getElementById('expected_cost').innerHTML = '$' + updates[update_index]['expected_cost'];
    document.getElementById('impact_discussion').innerHTML = updates[update_index]['impact_discussion'];
    document.getElementById('priority_effect').innerHTML = updates[update_index]['priority_effect'];
    document.getElementById('priority_monetary').innerHTML = updates[update_index]['priority_monetary'];
}
</script>

供您参考,以下是页面加载时的样子:

<script type="text/javascript">
var updates = new Array();
updates[0] = new Array();
updates[0]['date_of_update'] = '2013-08-26';
updates[0]['impact'] = 'delay in schedule';
updates[0]['probability'] = '18';
updates[0]['impact_effect'] = '79';
updates[0]['cost_impact'] = '21000.00';
updates[0]['overall_impact'] = '14';
updates[0]['expected_cost'] = '3780.00';
updates[0]['impact_discussion'] = 'Critical path items past schedule, invoke contract penalties. You can change a very limited number of settings related to formatting. open up netbeans IDE Go to tools->options click on Editor button on top left of the options dialog box click on lot';
updates[0]['priority_effect'] = '1';
updates[0]['priority_monetary'] = '1';

function update_fields() {

var update_index = document.getElementById('select_update').selectedIndex;
alert(update_index);
document.getElementById('impact').innerHTML = updates[update_index]['impact'];
document.getElementById('probability').innerHTML = updates[update_index]['probability'] + '%';
document.getElementById('impact_effect').innerHTML = updates[update_index]['impact_effect'];
document.getElementById('cost_impact').innerHTML = '$' + updates[update_index]['cost_impact'];
document.getElementById('overall_impact').innerHTML = updates[update_index]['overall_impact'];
document.getElementById('expected_cost').innerHTML = '$' + updates[update_index]['expected_cost'];
document.getElementById('impact_discussion').innerHTML = updates[update_index]['impact_discussion'];
document.getElementById('priority_effect').innerHTML = updates[update_index]['priority_effect'];
document.getElementById('priority_monetary').innerHTML = updates[update_index]['priority_monetary'];
document.getElementById('risk_statement').innerHTML = "If " + <?php
echo $risk_data['event'] . " by " .
 $risk_data['date_of_concern'];
?> + " then "  updates[update_index]['impact'] + ".";
}
</script>

但由于某种原因,脚本根本不运行。它应该由元素的“onchange”事件调用,但没有任何反应。我不确定错误是否在于我如何声明数组。如果有人有任何提示,我会非常感激!

1 个答案:

答案 0 :(得分:0)

JavaScript Arrays []不是关联的,并且完全不像PHP数组......

在PHP中,你可以这样做:

$array = array(
    "foo" => "bar",
    "bar" => "foo"
);

$array2 = array(
    "foo",
    "bar"
);

是关联关系还是非关联关系。

在JavaScript中,人们称之为关联数组的是Object-Literal(数组也是对象,但此时不要让你感到困惑)。

要模仿与上面相同的行为,你可以这样做:

var myArray = {
    "foo": "bar",
    "bar": "foo",
};

var myArray2 = [
    "foo",
    "bar"
];

使用JS对象文字(myArray),您可以使用点符号来定义属性:myArray.foo = "bar";以及myArray["foo"] = "bar";

但是对于数组,您需要使用.push()方法:myArray.push("bar");


虽然我不建议使用PHP来创建JavaScript对象,但您需要更改函数以使用对象文字而不是数组。

一些好的阅读:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects