I have an array built from the URL of a webpage.
If an item in that array contains the ?
symbol (The question mark symbol) then I want to remove that item from the array.
$array = ['news', 'artical', '?mailchimp=1'];
How could I do this? I've seen many examples where the searched string is the whole value, but not where it's just a single character or just part of the value.
Get the array and the index. Convert the array into IntStream using IntStream. range() method. Remove the specified index element using the filter() method.
The splice() function adds or removes an item from an array using the index. To remove an item from a given array by value, you need to get the index of that value by using the indexOf() function and then use the splice() function to remove the value from the array using its index.
Method 2: Using the pop() method: If we simply use pop() without passing any parameter, then it will remove the element from the last (n th) index. But if we specify the index value of the array, it will remove that particular element from the array.
If you know the value you want to remove from an array you can use the splice method. First you must identify the index of the target item. You then use the index as the start element and remove just one element. This is a simple example where the elements are integers.
We can also convert the array to a list and call its RemoveAt () method to remove the element’s first occurrence. Then, we convert the list back to the array using the ToArray () method. That’s all about removing the specified element from an array in C#. Average rating 4.68 /5. Vote count: 25 Thanks for reading.
It modifies the array on which it is invoked. If there are no elements, or the array length is 0, the method returns undefined. The splice method can be used to add or remove elements from an array. The first argument specifies the location at which to begin adding or removing elements.
Another solution is to use the Enumerable.Except () method, which compares two sequences and returns the elements that appear only in the first sequence. This method is demonstrated below: The following code example demonstrates how we can use Where () to remove only the first occurrence of an element from the array.
http://www.php.net/manual/en/function.array-filter.php
function myFilter($string) {
return strpos($string, '?') === false;
}
$newArray = array_filter($array, 'myFilter');
foreach($array as $key => $one) {
if(strpos($one, '?') !== false)
unset($array[$key]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With