1

I have an array in a php variable called $result.

When I do echo json_encode($result); I get:

[{"id":"4","rank":"adm","title":"title 1"},
{"id":"2","rank":"mod",,"title":"title 2"},
{"id":"5","rank":"das","title":"title 3"},
{"id":"1","rank":"usr""title":"title 4"},
{"id":"3","rank":"ref","title":"title 5"}]

However I get a different result when try to get same using alert. Here is the example.

<script type="text/javascript">
//<![CDATA[
$(document).ready(function() { myArray = <?=json_encode($result);?>; });
$('img.delete').click(function() { alert(myArray); }
//]]>
</script>

The alert that I get is [object Object],[object Object], ...

I am not sure, but it seems that the variable myArray is not properly getting carried to $('img.delete').click(function().

2
  • 1
    That doesn't look strange to me... Commented Feb 17, 2011 at 13:04
  • whats with the double comma on id2 and no comma on id1 (is that not invalid json) ... just saying, but i get what you're asking. Commented Feb 17, 2011 at 13:32

3 Answers 3

1

Invoking alert(myArray) doesn't give you the json representation of your data. The function alert() invokes toString() on its argument. And the default toString() implementation of an javascript object just returns the string "[object Object]".

If you want to display the JSON representation, then you can use a json serialization library to generate the json string. This page explains an example.

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

Comments

1

It seems to me that you have an array of objects, in the JS I would say you need to itterate through the objects and read the variables within, as the alert isn't displaying the contents of the object onthat that you've asked to show whats in the array - which is to say: and array of objects.

I would save the array to variable and itterate through the array and then output the array contained within for each.

um.... kinda like this:

for(i = 0; i < myArray.length; i++) {
 // access each sub object and collate the info you want to display
 // ie: myArray[i]['id']
}

I may have over-simplified it, but i can remember having the same issues with an ajax app i built and im sure this is the way i went about it.

4 Comments

kinda goes hand in hand with what @Briedis said below/above (or whereever it is)
Christopher, your answer is actually pretty well explained. Having said that, could you please thro' light on why this is not working? for(i = 0; i < myArray.length; i++) { if (myArray[i]['id'] == deltid) { myArray[i]['title'] = 'delete'; } }
well looks fine to me, what about it isnt working? im assuming that either deltid is a variable with the correct value, and that you havnt neglected to put == "deltid" but without more info i cant help... maybe start a new question, and comment the link here i'd be glad to help. ps: if my answer helped, then either an upvote or a accept would be awesome ;)
I wanted to give an upvote, but I don't have enough point for that, I need 15. Will come back soon to give you a upvote, as soon as I get 15 (I've 12 now). Many thanks again.
0

What would you expect?

Try echo array("1" => 1, "2" => 2); in php and see what you get.

myArray is a javascript object, not a string that you can easily output. If you want to output a certain value, then write alert(myArray[0]['id']);

The other thing - variable scope. You should declare myArray as a global if you want to use it in various closures.

3 Comments

Thanks Briedis. The oho echoing is working perfectly, as it should. alert(myArray[0]['id']); does make sense. However, is there a way to format myArray to JSON?
my answer was along these lines but possibly not explained well enough, though the itteration you could itterate through myArray[] and collate All the contents to be displayed once...
@Jeremy, what Briedis meant with echoing an array in PHP is that when you do that the output will be the string Array, not the entire array in a string. The same thing is happening in JavaScript.

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.