Remove empty array elements

As you’re dealing with an array of strings, you can simply use array_filter(), which conveniently handles all this for you: print_r(array_filter($linksArray)); Keep in mind that if no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed. So if you need to preserve elements that are i.e. exact … Read more

How to convert a Java 8 Stream to an Array?

The easiest method is to use the toArray(IntFunction<A[]> generator) method with an array constructor reference. This is suggested in the API documentation for the method. String[] stringArray = stringStream.toArray(String[]::new); What it does is find a method that takes in an integer (the size) as argument, and returns a String[], which is exactly what (one of … Read more

How can I access and process nested objects, arrays, or JSON?

Preliminaries JavaScript has only one data type which can contain multiple values: Object. An Array is a special form of object. (Plain) Objects have the form {key: value, key: value, …} Arrays have the form [value, value, …] Both arrays and objects expose a key -> value structure. Keys in an array must be numeric, … Read more

PHP array delete by value (not key)

Using array_search() and unset, try the following: if (($key = array_search($del_val, $messages)) !== false) { unset($messages[$key]); } array_search() returns the key of the element it finds, which can be used to remove that element from the original array using unset(). It will return FALSE on failure, however it can return a false-y value on success … Read more

How to determine if Javascript array contains an object with an attribute that equals a given value?

No need to reinvent the wheel loop, at least not explicitly (using arrow functions, modern browsers only): if (vendors.filter(e => e.Name === ‘Magenic’).length > 0) { /* vendors contains the element we’re looking for */ } or, better yet, use some as it allows the browser to stop as soon as one element is found … Read more

How to get the difference between two arrays in JavaScript?

There is a better way using ES7: Intersection let intersection = arr1.filter(x => arr2.includes(x)); For [1,2,3] [2,3] it will yield [2,3]. On the other hand, for [1,2,3] [2,3,5] will return the same thing. Difference let difference = arr1.filter(x => !arr2.includes(x)); For [1,2,3] [2,3] it will yield [1]. On the other hand, for [1,2,3] [2,3,5] will … Read more