空多维数组初始化省略指针

时间:2012-10-16 11:29:22

标签: javascript arrays

有没有办法在Javascript中初始化空数组,在php中初始化相同的空数组?

$blah = array("0" => "wew", "1" => "fdafa");

foreach ($blah as $blat => $val) {
              $arr[] = $val
              $arr[$blah][] = 1; 
}

或者只是

foreach ($blah as $blat => $val) {
                  $arr[] = $val
    }

1 个答案:

答案 0 :(得分:0)

不,您必须声明var并在将项目推送到[]之前将其初始化为var blah = {"0":"wew", "1":"fdafa"}; // an object, not an array var arr = []; var obj = {} for (var blat in blah) { var val = blah[blat]; arr.push(val); obj[blat] = 1; }

{{1}}