Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5984351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:21:49+00:00 2026-05-22T22:21:49+00:00

I seem to have a problem converting an array backwards and forwards between PHP/JS.

  • 0

I seem to have a problem converting an array backwards and forwards between PHP/JS. I’m using an XmlHttpRequest from JavaScript to a PHP page which uses json_encode to encode a multidimensional (2D) array.

When receiving the string, I use JSON.parse() to decode the string, but it comes back as a 1D array. Is there any way to parse a JSON string into a multidimensional array rather than single dimension?

Example of the JSON recieved (from a CSV file):

[
    {
        "rating": "0",
        "title": "The Killing Kind",
        "author": "John Connolly",
        "type": "Book",
        "asin": "0340771224",
        "tags": "",
        "review": "i still haven't had time to read this one..."
    },
    {
        "rating": "0",
        "title": "The Third Secret",
        "author": "Steve Berry",
        "type": "Book",
        "asin": "0340899263",
        "tags": "",
        "review": "need to find time to read this book"
    },
    {
        "rating": "3",
        "title": "The Last Templar",
        "author": "Raymond Khoury",
        "type": "Book",
        "asin": "0752880705",
        "tags": "",
        "review": ""
    },
    {
        "rating": "5",
        "title": "The Traveller",
        "author": "John Twelve Hawks",
        "type": "Book",
        "asin": "059305430X",
        "tags": "",
        "review": ""
    },
    {
        "rating": "4",
        "title": "Crisis Four",
        "author": "Andy Mcnab",
        "type": "Book",
        "asin": "0345428080",
        "tags": "",
        "review": ""
    },
    {
        "rating": "5",
        "title": "Prey",
        "author": "Michael Crichton",
        "type": "Book",
        "asin": "0007154534",
        "tags": "",
        "review": ""
    },
    {
        "rating": "3",
        "title": "The Broker (Paperback)",
        "author": "John Grisham",
        "type": "Book",
        "asin": "0440241588",
        "tags": "book johngrisham",
        "review": "good book, but is slow in the middle"
    },
    {
        "rating": "3",
        "title": "Without Blood (Paperback)",
        "author": "Alessandro Baricco",
        "type": "Book",
        "asin": "1841955744",
        "tags": "",
        "review": ""
    },
    {
        "rating": "5",
        "title": "State of Fear (Paperback)",
        "author": "Michael Crichton",
        "type": "Book",
        "asin": "0061015733",
        "tags": "",
        "review": ""
    },
    {
        "rating": "4",
        "title": "The Rule of Four (Paperback)",
        "author": "Ian Caldwell",
        "type": "Book",
        "asin": "0099451956",
        "tags": "book bestseller",
        "review": ""
    },
    {
        "rating": "4",
        "title": "Deception Point (Paperback)",
        "author": "Dan Brown",
        "type": "Book",
        "asin": "0671027387",
        "tags": "book danbrown bestseller",
        "review": ""
    },
    {
        "rating": "5",
        "title": "Digital Fortress : A Thriller (Mass Market Paperback)",
        "author": "Dan Brown",
        "type": "Book",
        "asin": "0312995423",
        "tags": "book danbrown bestseller",
        "review": ""
    },
    {
        "rating": "5",
        "title": "Angels & Demons (Mass Market Paperback)",
        "author": "Dan Brown",
        "type": "Book",
        "asin": "0671027360",
        "tags": "book danbrown bestseller",
        "review": ""
    },
    {
        "rating": "4",
        "title": "The Da Vinci Code (Hardcover)",
        "author": "Dan Brown",
        "type": "   Book   ",
        "asin": "0385504209",
        "tags": "book movie danbrown bestseller davinci",
        "review": ""
    }
]

Once parsed, if I try to access it using myArr[0][1], it shows as undefined.

Forgive me if this is obvious, I’m new to JS and JSON

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:21:49+00:00Added an answer on May 22, 2026 at 10:21 pm

    Edit: After your edit showing your actual JSON:

    The JSON you’ve defined isn’t defining a two-dimensional array, it’s defining a one-dimensional array of objects. Once deserialized, you’d access (say) the title of the first book like so:

    alert(myArray[0].title);
    

    Live example (note that in that example I’ve had to escape the single quotes in the JSON you gave in order to put the whole thing inside a JavaScript string; don’t let that throw you, your JSON text is correct and valid as you quoted it, it’s just that I’m putting it inside a string delimited with single quotes, so I have to escape the single quotes in it)


    Original answer:

    JavaScript arrays are always one-dimensional, but each entry in an array can, itself, be a reference to an array, which effectively makes for two-dimensional arrays. JSON has no problem with these when encoded properly:

    [
        [1, 2, 3],
        [4, 5, 6]
    ]
    

    That’s JSON notation for an array with two entries, each of which is an array with three entries.

    Example using Crockford’s JSON stringifier and parser (the syntax below is also now standard as of ECMAScript5, but not all browsers have it yet):

    var a, str, b;
    
    a = [
        [1, 2, 3],
        [4, 5, 6]
    ];
    display("a.length = " + a.length);
    display("a[0] = " + a[0].join(","));
    display("a[1] = " + a[1].join(","));
    str = JSON.stringify(a);
    display("JSON string: " + str);
    b = JSON.parse(str);
    display("b.length = " + b.length);
    display("b[0] = " + b[0].join(","));
    display("b[1] = " + b[1].join(","));
    

    Output:

    a.length = 2
    a[0] = 1,2,3
    a[1] = 4,5,6
    JSON string: [[1,2,3],[4,5,6]]
    b.length = 2
    b[0] = 1,2,3
    b[1] = 4,5,6

    Live copy

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to always have problems with converting data to and from XML in
I seem to have a problem with this SQL query: SELECT * FROM appts
I have a problem, which I do not seem to be able to solve...
I really seem to have a big problem here. I'm using MySQL to store
Since MySQL doesn't seem to have any 'boolean' data type, which data type do
I have a basic ajax application, which will not work, instead the php code
I seem to have the all-familiar problem of correctly reading and viewing a web
I have a bizarre problem: Somewhere in my HTML/PHP code there's a hidden, invisible
I seem to have a problem, I have a growing business to run but
I always seem to have a problem when I need to compare 2 list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.