Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove item from array if item value contains searched string character

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.

like image 686
Nicekiwi Avatar asked Apr 03 '12 12:04

Nicekiwi


People also ask

How do I remove an element from an array of strings?

Get the array and the index. Convert the array into IntStream using IntStream. range() method. Remove the specified index element using the filter() method.

How do you delete a matching element from an array?

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.

How do I remove a string from an array in Python?

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.

How do I remove a value from an 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.

How to remove the specified element from an array in C#?

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.

How to add or remove elements from an array in JavaScript?

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.

How to remove the first occurrence of an element from an array?

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.


Video Answer


2 Answers

http://www.php.net/manual/en/function.array-filter.php

function myFilter($string) {
  return strpos($string, '?') === false;
}

$newArray = array_filter($array, 'myFilter');
like image 64
Nameless Avatar answered Oct 07 '22 08:10

Nameless


foreach($array as $key => $one) {
    if(strpos($one, '?') !== false)
        unset($array[$key]);
}
like image 31
Mircea Soaica Avatar answered Oct 07 '22 08:10

Mircea Soaica