将Array插入另一个Array然后传递给PHP

时间:2014-01-28 16:05:42

标签: jquery arrays

我有以下代码:

d = (d == '') ? 0 : d;
m = (m == '') ? 0 : m;
s = (s == '') ? 0 : s;

dmsArray = new Array(d, m, s);
surveyBearing = new Array();
surveyBearing.push(dir1);
surveyBearing.push(dmsArray);
surveyBearing.push(dir2);

surveyBearings.push(surveyBearing);

dmsArray.length = 0;

我没有包括整体,但我会详细解释。变量(d,m,s,dir1,dir2)取自输入形式。 surveyBearing是一个将被推入surveyBearings数组的数组。但是,console.log(surveyBearings)显示:

[["S", [], "E"], ["S", [], "W"], ["N", [], "W"], ["N", [], "E"]]

我在这里遗漏了什么吗?我确信dmsArray不为空,因为console.log(dmsArray)返回

["9", 0, 0]

在PHP方面,它返回

Array
(
[0] => Array
    (
        [0] => S
        [1] => Array
            (
            )

        [2] => E
    )

[1] => Array
    (
        [0] => S
        [1] => Array
            (
            )

        [2] => W
    )

[2] => Array
    (
        [0] => N
        [1] => Array
            (
            )

        [2] => W
    )

[3] => Array
    (
        [0] => N
        [1] => Array
            (
            )

        [2] => E
    )

)

1 个答案:

答案 0 :(得分:0)

你需要更改你的var声明,以便像这样检查undefined状态:

d = (d == '' || typeof d === 'undefined') ? 0 : d;
m = (m == '' || typeof m === 'undefined') ? 0 : m;
s = (s == '' || typeof s === 'undefined') ? 0 : s;

Fiddle