How-to articles, tricks, and solutions about ARRAYS

Java String array: is there a size of method?

In Java, you can use the length field of an array to determine the size of the array. For example, given an array arr, you can get the size of the array using arr.length.

JSON_ENCODE of multidimensional array giving different results

JSON_ENCODE is a function that converts a PHP data structure, such as an array, into a JSON (JavaScript Object Notation) string.

Limit array to 5 items

You can use the "array_slice" function in PHP to limit an array to a specific number of items.

Make copy of an array

To make a copy of an array in Java, you can use the clone() method of the Object class. The clone() method creates a shallow copy of the array, which means that it creates a new array with the same elements as the original array

Make var_dump look pretty

To make the output of var_dump() more readable, you can use the following approaches:

Most efficient way to search for object in an array by a specific property's value

In PHP, you can use the built-in function array_filter() to filter the array by a specific property's value.

PHP - Check if two arrays are equal

You can use the array_diff() function to check if two arrays are equal.

PHP : Remove object from array

Here is an example of how to remove an object from an array of objects in PHP:

PHP append one array to another (not array_push or +)

To append one array to another in PHP, you can use the array_merge function.

PHP foreach with Nested Array?

To loop through a nested array in PHP using a foreach loop, you can use nested foreach loops.

PHP How to add to end of array?

In PHP, you can add an element to the end of an array using the array_push() function.

PHP sort array alphabetically using a subarray value

In PHP, you can use the "usort" function to sort an array by a specific value of a subarray.

PHP: Check if an array contains all array values from another array

You can use the array_diff function in PHP to compare two arrays and check if the first array contains all of the values from the second array.

Random shuffling of an array

To shuffle an array randomly in Java, you can use the Collections.shuffle method.

Remove all elements from array that do not start with a certain string

You can use the array_filter() function in PHP to remove all elements from an array that do not start with a certain string.

Removing an element from an Array in Java

There are a few different ways you can remove an element from an array in Java. Here are a few options:

Save PHP array to MySQL?

To save a PHP array to a MySQL database, you can use the serialize() function to convert the array into a string, and then use an INSERT or UPDATE statement to save the string into a TEXT or LONGTEXT field in the database.

search a php array for partial string match

You can use the array_filter() function in PHP to search through an array and return only the elements that contain a certain string.

search and replace value in PHP array

In PHP, you can use the array_map() function to search and replace values in an array.

Sort an array in Java

To sort an array in Java, you can use the Arrays.sort() method of the java.util.Arrays class. This method sorts the elements of the array in ascending order according to their natural ordering.

string to string array conversion in java

There are several ways to convert a string to a string array in Java. Here are a few options:

Twig for loop for arrays with keys

To loop through an array with keys in Twig, you can use the for loop and access the key and value of each element in the array using the loop variable.

Type hinting in PHP 7 - array of objects

In PHP 7, type hinting can be used to specify the expected data type of a function or method parameter.

ValueError: setting an array element with a sequence

This code creates a 2D numpy array, and then tries to set the first element of the array to a list.

What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

java.lang.ArrayIndexOutOfBoundsException is an exception that is thrown when you try to access an element of an array with an index that is out of bounds. This can happen if you try to access an index that is either negative or greater than or equal to