2

I insert several(array) value with json_encode in one row from database table, now want echo they as order with jquery.

This is output from my PHP code:

[{
    "guide": null,
    "residence": [{
        "name_r": "jack"
    }, {
        "name_r": "jim"
    }, {
        "name_r": "sara"
    }],
    "residence_u": [{
        "units": ["hello", "how", "what"],
        "extra": ["11", "22", "33"],
        "price": ["1,111,111", "2,222,222", "3,333,333"]
    }, {
        "units": ["fine"],
        "extra": ["44"],
        "price": ["4,444,444"]
    }, {
        "units": ["thanks", "good"],
        "extra": ["55", "66"],
        "price": ["5,555,555", "6,666,666"]
    }]
}]

I want as(output):

jack
hello & 11 & 1,111,111
how & 22 & 2,222,222
what & 33 & 3,333,333,

jim
fine & 44 & 4,444,444

sara
thanks & 55 & 5,555,555
good & 66 & 6,666,666

How is it?

1
  • @Matt Ball - What is the problem? What's your question? Commented Sep 24, 2011 at 22:52

1 Answer 1

2

Assuming

  • you already know how make the ajax request that fetches this information from your PHP script
  • there is always one element in the top-level array (if not, you could add one more level of iteration)
  • by output you mean access the corresponding elements - I've put in calls to console.log but you could alert() or put into a DOM element or whatever

you could do something like this in jQuery (here's an example fiddle without the network part. You'll see the output in your console)

var data = response[0]; //response is the data received by the jQuery ajax success callback
var residences = data.residence;
var residence_u = data.residence_u;

$.each(residences, function(index, val){
    var name = val.name_r;
    console.log(name);

    var info = residence_u[index]; //get the corresponding residence_u element

    $.each(info.units, function(index, val){
        var unit = val;
        var extra = info.extra[index];
        var price = info.price[index];
        console.log( val + " & " + extra + " & " + price);
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

When i want use from your code, it in my code not worked. see my full code: pastebin.com/Rmt1797X i want append result in <div class="tooltip">'+append here+'</div> in $.each(data[0].residence_u, function...
@EmmyCharles What doesn't work exactly? Please consider posting an SSCCE. Also note that an id can't legally have ,s in it - you're doing that when you use 'units` as the id.

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.