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 6804655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:29:35+00:00 2026-05-26T19:29:35+00:00

I am trying to use the data I have received from a php script,

  • 0

I am trying to use the data I have received from a php script, which was json_encode’d, but I just don’t seem able to get it right.

This is the javascript function I am using
JS:

function dispJSON(items) {
  console.log('Raw from PHP:' + items);
  var strJSON = JSON.stringify(items);
  console.log('Sent from PHP and stringified:' + strJSON);
  $('#results').append(strJSON + '<hr>');
  var opStr =  '';
  for (i=0; i<items.length; i++){    //Get each Record
    console.log('REC=' + items[i].tostring);
    for (var key in items[i]) {
      console.log(key + ' => ' + items[i][key]);
    }
  }
}

and this is what the console says.
Console:

WCIT.html:60  Sent from PHP and stringified:[[{"vehGrpName":"Austin","vehTitle":"Austin A30 (AS3 and AS4)","vehDescrip":"The new look Baby Austin to rival the Morris Minor."}],[{"vehGrpName":"Austin","vehTitle":"Austin A35","vehDescrip":"An improved A30.  Better brakes, gearbox and engine."}]]
WCIT.html:64  REC=undefined
WCIT.html:66  0 => [object Object]
WCIT.html:64  REC=undefined
WCIT.html:66  0 => [object Object]

So I have obviously misunderstood some basics. My javascript / json knowledge is very limited.

I seem to be getting One Array (All the records), made up of two arrays (ie each record, which contains a json object of the record)

My code is obviously garbage, so if anyone can put me right, I would be most grateful.

Thanks

  • 1 1 Answer
  • 0 Views
  • 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-26T19:29:36+00:00Added an answer on May 26, 2026 at 7:29 pm

    In your dispJSON function, you’re no longer dealing with JSON (which is a textual data notation) at all — something somewhere along the line has deserialized it for you (which is handy). You’re dealing with the resulting array. From the look of the stringified copy, it’s an array of arrays, and each entry in the sub-arrays (which oddly only have one entry each) is an object with various properties:

    [
        [
            {
                "vehGrpName": "Austin",
                "vehTitle": "Austin A30 (AS3 and AS4)",
                "vehDescrip": "The new look Baby Austin to rival the Morris Minor."
            }
        ],
        [
            {
                "vehGrpName": "Austin",
                "vehTitle": "Austin A35",
                "vehDescrip": "An improved A30.  Better brakes, gearbox and engine."
            }
        ]
    ]
    

    (jsonlint is handy for pretty-printing structures.)

    So you’ll want to loop through that and deal with each of the sub-arrays and their entries (live copy):

    function dispStuff(items) {
        var outer, inner, sub, entry;
    
        for (outer = 0; outer < items.length; ++outer) {
            sub = items[outer];
            for (inner = 0; inner < sub.length; ++inner) {
                entry = sub[inner];
                console.log("Entry " + outer + "," + inner + ":");
                console.log("vehGrpName = " + entry.vehGrpName);
                console.log("vehTitle = " + entry.vehTitle);
                console.log("vehDescrip = " + entry.vehDescrip);
            }
        }
    }
    

    Or if you don’t want to have the entry keys in your code (live copy):

    function dispStuff(items) {
        var outer, inner, sub, entry, name;
    
        for (outer = 0; outer < items.length; ++outer) {
            sub = items[outer];
            for (inner = 0; inner < sub.length; ++inner) {
                entry = sub[inner];
                console.log("Entry " + outer + "," + inner + ":");
                for (name in entry) {
                  console.log(name + " = " + entry[name]);
                }
            }
        }
    }
    

    That works because for..in loops through the names of the properties in an object (as well as the property names of any of its prototype objects), and because you can refer to an object property in two different ways: In the familiar obj.propName form where propName is a literal, or in the obj["propName"] form, where the property name is a string. And of course, since it’s a string, it can be the result of any expression.


    Separately: You might want to find out why your PHP code is creating the sub-arrays with one entry each…

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

Sidebar

Related Questions

I have received data from UDP socket fd . How can I use this
I have application that received data via HTTP POST requests. I'm trying to use
I'm trying to use NHibernate for data access, and I have 2 simple entities
So, I'm trying to use Doctrine to retrieve some data. I have some basic
I'm trying to use some data from a PlanPlusOnline account. They only provide a
I'm trying to use Haskells Data.Heap module, but I'm incapable of even using it
I am trying to use the new DATETIMEOFFSET data type in SQL 2008 but
We have received a WSDL from our client that we use to communicate with
I am trying to use AutoMapper to map some DTO (data contract) objects received
I'm trying to use WCF Data Service with Subsonic, but ran into this error

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.