Home » HTML » HTML_AJAX » Bug #6737
When I return an array it has an extra element.
Details
| Submitted | 2006-02-09 19:22 UTC |
|---|---|
| From | dave dot rodal at gmail dot com |
| Assigned | auroraeosrose |
| Status | Closed |
| Package | HTML_AJAX |
| PHP Version | 4.3.11 |
| OS | fedora core 3 |
| Roadmaps | 0.5.1 |
Comments
[2006-02-09 19:22 UTC] dave dot rodal at gmail dot com
Description:
------------
I take you hello world example and make randomString return
an array. I print out the array with a Javascript
for (var v in obj) script.
If I return an associative array, this works fine.
If I return a numbered array I get an extra datum in the printout like this
1
2
3
______array
The _____array get printed out as an extra element.
See the testscript part for changes I made to the script.
Now if I return a numbered array and print it out with javascript code like this
for(var i;i < obj.length;i++){
...innerHTML += obj[i];
}
All prints out nicely, no _____array.
Test script:
---------------
function randomString() {
$index = rand(0,count($_SESSION['strings'])-1);
return array(1,2,3);
//return //array("First"=>"FUN","Second"=>"Joy","Three"=>"Toys");
//return $_SESSION['strings'][$index];
}
And the change I made to index.php
randomstring: function(result) {
for(var i in result){
document.getElementById('canvas').innerHTML += '<p>'+result[i]+'</p>';
}
},
Expected result:
----------------
1
2
3
Actual result:
--------------
1
2
3
______array
[2006-02-09 19:39 UTC] jeichorn at php dot net
Quick note you shouldn't use for(var i in object) to loop over an array. An array is an object in JS and can have any number of additional properties added too it. Use for(var i = 0; i < array.length; i++).
If we want to look into getting rid of the ____array property this bug needs to actually get moved over to the Services_JSON guys since were just bundling their code until they have a release.
[2006-02-09 21:43 UTC] jeichorn at php dot net
It was thanks, i was looking for Services_JSON but it doesn't have an entry. Must have got it switched to SOAP somehow
[2006-11-29 15:54 UTC] auroraeosrose at php dot net
bundled newer version of crockford's JSON class that doesn't mess with prototypes