Remove Item from Array If It Exists in a 'Disallowed Words' Array

Remove item from array if it exists in a 'disallowed words' array

You want array_diff.

array_diff returns an array containing
all the entries from array1 that are
not present in any of the other
arrays.

So you want something like:

$good = array_diff($arr, $disallowed_words);

Removing items from an array if they exist in another array

You can use array_diff():

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

$filteredFoo = array_diff($foo, $bar);

Remove elements of one array if it is found in another

You are looking for array_diff:

$subject = "Warmly little in before cousin as sussex...";
$tags = explode(" ", strtolower($subject));

$definite_articles = array('the','this','then','there','from','for','to','as');

$tags = array_diff($tags, $definite_articles);
print_r($tags);

See it in action.

Remove array elements that are present in another array

You can use .reject to exclude all banned words that are present in the redacted array:

words.reject {|w| redacted.include? w}

Demo

If you want to get the list of banned words that are present in the words array, you can use .select:

words.select {|w| redacted.include? w}

Demo

how to remove an item in a javascript array with json form

You could use a property filter to match the item in your history list. Below is the sample quick code to do so, I've modified your history list a bit.

A quick test in FF, shows that it works!

var historyList = []; // assuming this already has the array filled

function addToHistory(name, notes) {
var newHistory = {
ID: new Date().getTime(),
Name: name,
DoseDate: "Somedate",
ResultDate: "SomeResultDate",
Notes: notes,
toString: function() {return this.Name;}
};
historyList.push(newHistory);
};

var Util = {};

/**
* Gets the index if first matching item in an array, whose
* property (specified by name) has the value specified by "value"
* @return index of first matching item or false otherwise
*/
Util.arrayIndexOf = function(arr, filter) {
for(var i = 0, len = arr.length; i < len; i++) {
if(filter(arr[i])) {
return i;
}
}
return -1;
};

/**
* Matches if the property specified by pName in the given item,
* has a value specified by pValue
* @return true if there is a match, false otherwise
*/
var propertyMatchFilter = function(pName, pValue) {
return function(item) {
if(item[pName] === pValue) {
return true;
}
return false;
}
}

/**
* Remove from history any item whose property pName has a value
* pValue
*/
var removeHistory = function(pName, pValue) {
var nameFilter = propertyMatchFilter(pName, pValue);
var idx = Util.arrayIndexOf(historyList, nameFilter);
if(idx != -1) {
historyList.splice(idx, 1);
}
};

// ---------------------- Tests -----------------------------------
addToHistory("history1", "Note of history1");
addToHistory("history2", "Note of history2");
addToHistory("history3", "Note of history3");
addToHistory("history4", "Note of history4");

alert(historyList); // history1,history2,history3,history4

removeHistory("Name", "history1");

alert(historyList); // history2,history3,history4

removeHistory("Notes", "Note of history3");

alert(historyList); // history2,history4

Excluding certain words from an array

Several ways to do this, if you still want to count them but just not display them in the table, you could do:

$blacklist = array('the', 'is', 'a');

foreach($result as $word => $count1)
{
if (in_array($word, $blacklist)) continue;

...

if you don't even want to count them, you can skip them in a similar way in your counting loop.

How to remove item from array if repeated more than specific quantity in PHP?

  1. Count occurrences of each value (e.g. using array_count_values)
  2. Either create new array, to where only allowed values are copied; or modify the source array removing unwanted items.
<?php
$org_array = ["dog","cat","dog","cat","elephant","dog","bird","bird","frog","bird","frog","bird"];

$val_count = array_count_values($org_array);

$repeat = 4;

$array4repeat = array();

foreach($org_array as $v) {
if ($val_count[$v] < $repeat) $array4repeat[] = $v;
}

print_r($array4repeat);

prints

Array
(
[0] => dog
[1] => cat
[2] => dog
[3] => cat
[4] => elephant
[5] => dog
[6] => frog
[7] => frog
)

Delete values of array two that are in array one in php

You can use array_diff():

$array2 = array_diff($array2, $array1);

Edit: Here is an example:

$array1 = array('zero', 'one', 'two', 'three');
$array2 = array('zero', 'test1', 'test2', 'three');

$array2 = array_diff($array2, $array1);
print_r($array2);

How to filter an array based on words in another array?

Like this?

function filterDefinition (defs, badWords) {
return defs.filter(function(def) {
return !badWords.some(badWord => def.includes(badWord) || def.includes(badWord.toUpperCase()));
});
}

String.includes(string) does not work on arrays.



Related Topics



Leave a reply



Submit