Currently, empty arrays are converted to JavaScript objects by drupal_to_js(). This patch fixes that behavior.

CommentFileSizeAuthor
#2 drupal_to_js_0.patch770 byteskkaefer
drupal_to_js.patch756 byteskkaefer

Comments

drumm’s picture

Version: x.y.z » 5.x-dev
Status: Needs review » Needs work

I'd rather see something like count($var) > 0, which would be easier to understand on reading code.

Why shouldn't empty arrays be converted? I'd think an empty array in PHP should be an empty array in JS.

kkaefer’s picture

StatusFileSize
new770 bytes

Why shouldn’t empty arrays be converted? I’d think an empty array in PHP should be an empty array in JS.

That’s the point. Currently, empty PHP arrays are converted to JavaScript objects (e.g. { }) instead of JavaScript arrays ([]).

I’d rather see something like count($var) > 0, which would be easier to understand on reading code.

Yes, that one is easier to read. Changed it in the attached patch. If the array length is zero, an empty array instead of an empty object is returned. If the array length is not zero and the indices are sequential numbers, an array is returned. If they are not, an object (hash) is returned.

kkaefer’s picture

Status: Needs work » Closed (duplicate)

http://drupal.org/node/121876 was committed which does exactly the same.