Convert array to JSON

Script for backward-compatibility: https://github.com/douglascrockford/JSON-js/blob/master/json2.js And call: var myJsonString = JSON.stringify(yourArray); Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing. Credit goes to: @Spudley for his comment below

Array.size() vs Array.length

Array.size() is not a valid method Always use the length property There is a library or script adding the size method to the array prototype since this is not a native array method. This is commonly done to add support for a custom getter. An example of using this would be when you want to … Read more

How to convert an array to object in PHP?

In the simplest case, it’s probably sufficient to “cast” the array as an object: $object = (object) $array; Another option would be to instantiate a standard class as a variable, and loop through your array while re-assigning the values: $object = new stdClass(); foreach ($array as $key => $value) { $object->$key = $value; } As … Read more

How can I convert the “arguments” object to an array in JavaScript?

ES6 using rest parameters If you are able to use ES6 you can use: Rest Parameters function sortArgs(…args) { return args.sort(function (a, b) { return a – b; }); } document.body.innerHTML = sortArgs(12, 4, 6, 8).toString(); As you can read in the link The rest parameter syntax allows us to represent an indefinite number of … Read more

How to get character array from a string?

Note: This is not unicode compliant. “I💖U”.split(”) results in the 4 character array [“I”, “�”, “�”, “u”] which can lead to dangerous bugs. See answers below for safe alternatives. Just split it by an empty string. var output = “Hello world!”.split(”); console.log(output); See the String.prototype.split() MDN docs.

What is array to pointer decay?

It’s said that arrays “decay” into pointers. A C++ array declared as int numbers [5] cannot be re-pointed, i.e. you can’t say numbers = 0x5a5aff23. More importantly the term decay signifies loss of type and dimension; numbers decay into int* by losing the dimension information (count 5) and the type is not int [5] any … Read more