views:

51

answers:

4

Sorry for asking a basic question like this but I've been reading on this for a few hours now and the examples aren't making sense to me in context of the way my array is.. I have been able to pass this from my server side (cakephp) to the javascript (I'm using jquery) but after that, I am lost as to how to make use of the data.. I tried just showing it using an alert, but it gives output like "object Object" and thats all..

This is an example array that I have:

    Array ( 

    [0] => Array ( [Uiemail] => Array ( [uiemail_id] => 2 [divid] => upd1 [width] => 200 [height] => 200 [leftpos] => 122 [toppos] => 122 [cssgroup] => 1 [colortop] => [colorbottom] => [colorborder] => [borderwidth] => [zindex] => ) ) 

    [1] => Array ( [Uiemail] => Array ( [uiemail_id] => 3 [divid] => upd2 [width] => 200 [height] => 200 [leftpos] => 333 [toppos] => 444 [cssgroup] => 1 [colortop] => [colorbottom] => [colorborder] => [borderwidth] => [zindex] => ) ) 

    [2] => Array ( [Uiemail] => Array ( [uiemail_id] => 4 [divid] => upd3 [width] => 200 [height] => 200 [leftpos] => 555 [toppos] => 466 [cssgroup] => 1 [colortop] => [colorbottom] => [colorborder] => [borderwidth] => [zindex] => ) ) 

) 

EDIT:

The array is the way it is as its the output from a Cakephp database GET (cakephp's equivalent of this).

It has 3 arrays, the first is the "parent array" and contains the [1], [2], [3] which are like an auto-increment ID for the 2nd tier arrays.

The 2nd tier has an array that holds the 3rd, this second doesn't really contain any data other than the 3rd array (its done like this by cakephp since its representing the database table)

So, the 3rd tier array has all the data that I need, however I also would want to use the first tier ID

IN PHP A INDIVIDUAL PIECE OF DATA FROM THIS ARRAY IS ACCESSED AS SUCH:

$arrayline = [0]['Uiemail']['toppos'];

Here is some javascript I tried that didn't work (json is array name):

    $(json).each(function() {
      alert(this.text)
    });
});

Thanks for any advice, its just that my array is a nested array (as its from a database call) and it seems most examples don't go this deep which is leading me to confusion.

A: 

You have to use jQuery.each() for that.

Reigel
I was looking at jquery .each but the examples they offer can't seem to help me figure out how to do this type of nested array, I will look over it again to see if I can post a better attempt here at what I am trying to do
Rick
+1  A: 

Try this:

$.each(json, function(index, element){
  alert(element);
});

if json is your javascript object which (hopefully) already was parsed as json, you can't call the jQuery constructor function on it and expect it to work like this.

Use the $.each() helper function to iterate over an object with jQuery.

If you still got [object Object] as alert, it's because those sub elements are Arrays or real Objects themself, so you need to dig deeper. Good help is FireBug and console.dir()

Reference: $.each()

jAndy
thanks, I will try that
Rick
When I do this I still just get [object Object]I am editing my post to explain more about the array structure as it essentially has 3 arrays for each set of entries which is kind of overly complex but its the way cakephp puts out the data
Rick
alert(element.text);
nathan
@Rick: no matter what data structure is created, use `FireFox` or `Chrome` and call `console.dir(json)`. This will show you that object in detail and gives you a better understanding of how to access a single subobject/array for instance.
jAndy
+1  A: 

I don't really understand your array notation...but my guess is that the representation in json/javascript would be:

[
    [[2, 'udp1', 200, 200, 122, ....]],
    [[3, 'udp2', 200, 200, 333, ....]],
    [[4, 'udp3', 200, 200, 555, ....]]
]

When you pass an array in json, the 'index names' aren't passed.

If this is the structure, then something like this might get something:

$.each(json, function(index, element){
    $.each(element, function(index, sub){
      alert(sub);
    });
});
sje397
Thanks for the post.. I updated my OP to better explain the array
Rick
@Rick - given that, I suspect my structure above is still correct. You see in my code example how another 'each' call is added inside the first. As jAndy suggested, depending on how deeply the nesting goes, you just need to keep adding another level of the each call to match the structure. Notice how the first level has json and element, and the second has element and sub...the third would have sub and x, the fourth x and x1, etc etc.
sje397
Thanks, will try that.. I didn't get a chance to last night as it was too late
Rick
+1  A: 

An 'associative array' in PHP (i.e. with meaningful keys like your 'uiemail_id' will become an object in JSON notation. Even an array that is not exactly 0-indexed & continuous will also become one. (i.e. an array with keys 0,1,2 will result in an array, keys 1,2,3 or 0,2,3 will result in an object.

If you access a value in PHP like this:

$arrayline = $array[0]['Uiemail']['toppos'];

The access on the resulting structure in js would be:

arrayline = jsonvar[0].Uiemail.toppos;

Enabing firebug in firefox, with breakpoints, lets you easily inspect any variable available at that moment at your leisure, which makes it a far easier to debug then the usual default PHP way of just dumping variable notation to stdout (i.e. printr / var_dump's).

Wrikken
thanks, that helps me understand it a lot better, I didn't know I could view it in firebug.. will try that
Rick