How-to articles, tricks, and solutions about ARRAYS

How can I force PHP to use strings for array keys?

In PHP, array keys are typically automatically cast to integers if they are numeric, and to strings if they are not.

How can I print all the values of an array?

In PHP, you can use a foreach loop to iterate through an array and print each value.

How can I trim all strings in an Array?

You can use the array_map() function to trim all strings in an array in PHP.

How do I access the ith column of a NumPy multidimensional array?

You can access the ith column of a NumPy multidimensional array by using the following syntax:

How do I declare and initialize an array in Java?

There are several ways to declare and initialize an array in Java. Here are a few examples: Declare and initialize an array of integers with size 5

How do I determine whether an array contains a particular value in Java?

To check if an array contains a particular value in Java, you can use the contains() method of the List interface, which is implemented by the ArrayList class.

How do I initialize a byte array in Java?

To initialize a byte array in Java, you can use the array initializer syntax, like this:

How do I print the full NumPy array, without truncation?

To print the full NumPy array without truncation, you can use the numpy.set_printoptions() function and set the threshold parameter to np.inf.

How do I reverse an int array in Java?

To reverse an int array in Java, you can use a loop to swap the elements at the beginning and end of the array, and then move the pointers inward until they meet in the middle of the array.

How do you reindex an array in PHP but with indexes starting from 1?

You can use the array_values function to get a new array with the values of the original array, and then use the array_combine function to create an array using the new values and an indexed array of keys starting from 1:

How do you remove an array element in a foreach loop?

It is generally not recommended to remove array elements while iterating over the array using a foreach loop, because the loop will behave unexpectedly when elements are removed.

How to "flatten" a multi-dimensional array to simple one in PHP?

In PHP, you can use the "array_reduce" function to flatten a multi-dimensional array.

How to access mysql result set data with a foreach loop

To access MySQL result set data with a foreach loop, you can use a loop like the following:

How to add new elements to an array in Java?

To add new elements to an array in Java, you have a few options: If you know the size of the array in advance and the array is not full, you can simply assign a new value to an unused element in the array.

How to check if PHP array is associative or sequential?

To check if an array is associative or sequential in PHP, you can use the array_keys() function and compare the resulting array of keys with the original array.

How to check if variable is array?... or something array-like

In PHP, you can use the "is_array" function to check if a variable is an array.

How to convert a char array back to a string?

To convert a char array back to a String in Java, you can use the String class's constructor that takes a char array as an argument.

How to convert a Java 8 Stream to an Array?

To convert a Java 8 Stream to an array, you can use the toArray() method of the Stream interface.

How to convert an Array to a Set in Java

To convert an array to a Set in Java, you can use the Arrays.asList() method to create a List from the array, and then use the List.toSet() method to create a Set from the List.

How to convert an ArrayList containing Integers to primitive int array?

To convert an ArrayList containing Integer objects to a primitive int array in Java, you can use the toArray method and a casting operation.

How to convert an int array to String with toString method in Java

To convert an int array to a string in Java, you can use the Arrays.toString() method from the java.util package.

How to convert array to list in Java

To convert an array to a list in Java, you can use the Arrays.asList() method. This method returns a fixed-size list backed by the specified array. Here's an example:

How to convert int[] into List<Integer> in Java?

To convert an int[] array into a List<Integer> in Java, you can use the Arrays.stream() method to create a stream of the array, and then use the mapToObj() method to map each element of the stream to an Integer object.

How to convert Java String into byte[]?

To convert a Java string into a byte array, you can use the getBytes() method of the java.lang.String class. This method returns an array of bytes representing the string in a specific encoding.