0

I am trying to display a multi-dimensional array in javascript. Could someone help me about my code? Thanks.

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.

    }

}

updated:

my array

The array was created from php then use json_encode to parse to 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
        )

)
6
  • 1
    Could you post rendered array (ie view source from your html) Commented Jul 5, 2012 at 14:59
  • post the json_encoded data, too, if possible, because your edit shows -I think- a php var_dump Commented Jul 5, 2012 at 15:04
  • @EliasVanOotegem not sure how to output my json_encoded data. little help? Thanks. Commented Jul 5, 2012 at 15:16
  • 1
    well, seeing as you json_encode it, and the 2nd level of your array is an iterable object, it might just be that array[i][a] will work, but to be sure: just add this to your php script: echo json_encode($yourPhpArray);, and paste the output here, or here this page will show you how the JSON string is structured, and how JS will parse it Commented Jul 5, 2012 at 15:38
  • 1
    When I said array[i][a] might work, I meant to say array[i][a][0], the simpleXML class implements the Iterable interface, so it might be that each node is converted to an array with its contents as only element. The reason why array[0][0] does work could have something to do with the "main" tag XML documents always have. But since you're using XML anyway: why not send the XML and parse it with JS? sure there could be a bit more overhead, but in today's world of broadband internet, that shouldn't be that big of a deal Commented Jul 5, 2012 at 15:58

4 Answers 4

3

Code works fine, your array must be invalid.

http://jsfiddle.net/DZwaK/

Sign up to request clarification or add additional context in comments.

1 Comment

yes. it seems like my array is invalid. I just post it in my updated section. not sure what's wrong. +1 though.
1

This works fine for me, used array = [[1,2],[3,4]]; as a test array, output: 1234. what do you get when you do document.write(JSON.parse(array)); or console.log(array);?

Comments

1

Try this

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;
}

1 Comment

It seems like your code work on normal array. I just pose my array in updated section. Still don't know the reason it fail. +1 though.
0

I usually use for each for an array, in js you can do like this

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.
    }
}

Someone said this is not safe but it still work :D

1 Comment

It isn't safe at all: using for...in on an array is like using a hammer to fasten a bolt: not the right tool for the job. for in is for objects (assoc arrays). Even then, you have to check objectVar.hasOwnProperty(i) to avoid prototype methods/variables to be processed in the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.