0

javascriptで多次元配列を表示しようとしています。誰かが私のコードについて助けてくれますか? ありがとう。

for (var i=0;i<array.length;i++){

    for (var a=0;a<array[i].length;a++){

       //document.write(array[0][0])    //this will have output
       document.write(array[i][a])      //this won't.

    }

}

更新しました:

私の配列

配列はphpから作成され、json_encodeを使用してJSに解析します。

   Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => In UK, HTC Defeats Apple's "Obvious" Slide Unlock Patent
        )

    [1] => SimpleXMLElement Object
        (
            [0] => timothy
        )

    [2] => SimpleXMLElement Object
        (
        )

    [3] => SimpleXMLElement Object
        (
            [0] => 2012-07-05T14:10:00+00:00
        )

    [4] => SimpleXMLElement Object
        (
            [0] => WikiLeaks Begins Release of 2.5m Syrian Emails
        )

    [5] => SimpleXMLElement Object
        (
            [0] => timothy
        )

    [6] => SimpleXMLElement Object
        (
        )

    [7] => SimpleXMLElement Object
        (
            [0] => 2012-07-05T13:29:00+00:00
        )

    [8] => SimpleXMLElement Object
        (
            [0] => A Critical Examination of Bill Gates' Philanthropic Record
        )

    [9] => SimpleXMLElement Object
        (
            [0] => samzenpus
        )

    [10] => SimpleXMLElement Object
        (
        )

    [11] => SimpleXMLElement Object
        (
            [0] => 2012-07-05T12:07:00+00:00
        )

    [12] => SimpleXMLElement Object
        (
            [0] => Ask Slashdot: How Does Your Company Evaluate Your Performance?
        )

    [13] => SimpleXMLElement Object
        (
            [0] => samzenpus
        )

    [14] => SimpleXMLElement Object
        (
        )

    [15] => SimpleXMLElement Object
        (
            [0] => 2012-07-05T08:52:00+00:00
        )

    [16] => SimpleXMLElement Object
        (
            [0] => UAV Cameras an Eye In the Sky For Adventurous Filmmakers
        )

    [17] => SimpleXMLElement Object
        (
            [0] => samzenpus
        )

    [18] => SimpleXMLElement Object
        (
        )

    [19] => SimpleXMLElement Object
        (
            [0] => 2012-07-05T05:55:00+00:00
        )

    [20] => SimpleXMLElement Object
        (
            [0] => Copyrights To Reach Deep Space
        )

    [21] => SimpleXMLElement Object
        (
            [0] => samzenpus
        )

    [22] => SimpleXMLElement Object
        (
        )

    [23] => SimpleXMLElement Object
        (
            [0] => 2012-07-05T02:46:00+00:00
        )

    [24] => SimpleXMLElement Object
        (
            [0] => FDA Approves HIV Home-Use Test Kit
        )

    [25] => SimpleXMLElement Object
        (
            [0] => samzenpus
        )

    [26] => SimpleXMLElement Object
        (
        )

    [27] => SimpleXMLElement Object
        (
            [0] => 2012-07-05T00:13:00+00:00
        )

    [28] => SimpleXMLElement Object
        (
            [0] => Texas Scientists Regret Loss of Higgs Boson Quest
        )

    [29] => SimpleXMLElement Object
        (
            [0] => samzenpus
        )

    [30] => SimpleXMLElement Object
        (
        )

    [31] => SimpleXMLElement Object
        (
            [0] => 2012-07-04T23:25:00+00:00
        )

    [32] => SimpleXMLElement Object
        (
            [0] => Icelandic MP Claims US Vendetta Against WikiLeaks
        )

    [33] => SimpleXMLElement Object
        (
            [0] => Soulskill
        )

    [34] => SimpleXMLElement Object
        (
        )

    [35] => SimpleXMLElement Object
        (
            [0] => 2012-07-04T22:38:00+00:00
        )

    [36] => SimpleXMLElement Object
        (
            [0] => Microsoft's 'Cannibalistic Culture'
        )

    [37] => SimpleXMLElement Object
        (
            [0] => Soulskill
        )

    [38] => SimpleXMLElement Object
        (
        )

    [39] => SimpleXMLElement Object
        (
            [0] => 2012-07-04T21:50:00+00:00
        )

    [40] => SimpleXMLElement Object
        (
            [0] => Android 4.1 Jelly Bean Review
        )

    [41] => SimpleXMLElement Object
        (
            [0] => Soulskill
        )

    [42] => SimpleXMLElement Object
        (
        )

    [43] => SimpleXMLElement Object
        (
            [0] => 2012-07-04T21:04:00+00:00
        )

    [44] => SimpleXMLElement Object
        (
            [0] => John the Ripper Cracks Slow Hashes On GPU
        )

    [45] => SimpleXMLElement Object
        (
            [0] => Soulskill
        )

    [46] => SimpleXMLElement Object
        (
        )

    [47] => SimpleXMLElement Object
        (
            [0] => 2012-07-04T20:38:00+00:00
        )

)
4

4 に答える 4

3

コードは正常に動作します。配列が無効である必要があります。

http://jsfiddle.net/DZwaK/

于 2012-07-05T14:55:31.960 に答える
1

これを試して

function writeMultiArrayToConsole(arr)
{
var console = document.getElementById('mydiv');
var msg = '';
for(var i=0;i<arr.length;i++)
{
    for(var j=0;j<arr[0].length;j++)
        msg += arr[i][j];
    msg += '<br />';
}
console.innerHTML = msg;
}
于 2012-07-05T14:59:42.217 に答える
1

これは私にとってはうまく機能array = [[1,2],[3,4]];し、テスト配列 output: として使用されます1234。またはを行うと何が得られますdocument.write(JSON.parse(array));console.log(array);

于 2012-07-05T14:54:43.323 に答える
0

私は通常、配列に for each を使用しますが、js では次のように実行できます

var array = [];
array[0] = ['foo', 'bar'];
array[1] = ['baz'];
for (i in array){
    for (j in array[i]){
       document.write(array[i][j])      //this won't.
    }
}

誰かがこれは安全ではないと言いましたが、それでも機能します :D

于 2012-07-05T15:00:58.300 に答える