0

有没有办法在 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
    }
4

1 回答 1

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; 
}
于 2012-10-16T11:34:53.703 回答