arrays


javascript - Return index of greatest value in an array

This is probably the best way, since it’s reliable and works on old browsers: function indexOfMax(arr) { if (arr.length === 0) { return -1; } var max = arr[0]; var maxIndex =... Read More


Is there a better PHP way for getting default value by key from array (dictionary)?

Time passes and PHP is evolving. PHP 7 now supports the null coalescing operator, ??: // Fetches the value of $_GET['user'] and returns 'nobody' // if it does not exist. $username = $_GET['user'] ??... Read More


c - Can't understand this way to calculate the square of a number

Obviously a hack... but a way of squaring a number without using the * operator (this was a coding contest requirement). (&a)[n] is equivalent to a pointer to int at location (a + sizeof(a[n])*n)... Read More


python - Concatenate two NumPy arrays vertically

Because both a and b have only one axis, as their shape is (3), and the axis parameter specifically refers to the axis of the elements to concatenate. this example should clarify what concatenate is... Read More


jquery - Pass array to ajax request in $.ajax()

info = []; info[0] = 'hi'; info[1] = 'hello'; $.ajax({ type: "POST", data: {info:info}, url: "index.php", success: function(msg){ $('.answer').html(msg); } });... Read More


ruby - Array include any value from another array?

(cheeses & foods).empty? As Marc-André Lafortune said in comments, & works in linear time while any? + include? will be quadratic. For larger sets of data, linear time will be faster. For small data... Read More


python - How to turn a boolean array into index array in numpy

Another option: In [13]: numpy.where(mask) Out[13]: (array([36, 68, 84, 92, 96, 98]),) which is the same thing as numpy.where(mask==True).... Read More


javascript - Converting Object to Array using ES6 features

Use (ES5) Array::map over the keys with an arrow function (for short syntax only, not functionality): let arr = Object.keys(obj).map((k) => obj[k]) True ES6 style would be to write a generator, and... Read More


PHP: Split a string in to an array foreach char

You can access characters in strings in the same way as you would access an array index, e.g. $length = strlen($string); $thisWordCodeVerdeeld = array(); for ($i=0; $i<$length; $i++) { $thisWordC... Read More


Find length of 2D array Python

Like this: numrows = len(input) # 3 rows in your example numcols = len(input[0]) # 2 columns in your example Assuming that all the sublists have the same length (that is, it's not a jagged array).... Read More