Asked  7 Months ago    Answers:  5   Viewed   46 times

I have an array like this:

array("a" => 2, "b" => 4, "c" => 2, "d" => 5, "e" => 6, "f" => 2)

Now I want to filter that array by some condition and only keep the elements where the value is equal to 2 and delete all elements where the value is NOT 2.

So my expected result array would be:

array("a" => 2, "c" => 2, "f" => 2)

Note: I want to keep the keys from the original array.

How can I do that with PHP? Any built-in functions?

 Answers

60
$fullArray = array('a'=>2,'b'=>4,'c'=>2,'d'=>5,'e'=>6,'f'=>2);


function filterArray($value){
    return ($value == 2);
}

$filteredArray = array_filter($fullArray, 'filterArray');

foreach($filteredArray as $k => $v){
    echo "$k = $v";
}
Wednesday, March 31, 2021
 
phirschybar
answered 7 Months ago
57

You can use array_filter and stripos

$array = array(1 => 'January', 'February', 'March');
print_r(array_filter($array, function ($var) { return (stripos($var, 'Jan') === false); }));
Friday, May 28, 2021
 
Tapha
answered 5 Months ago
88
  1. Boolean is a function. It's the function you're calling indirectly through noisy. A bit confusing, I know, because it looks like the name of a type. But in JavaScript, those initially-capped things (Boolean, Number, String, and so on) are functions. When you call Boolean (without using new), it tries to convert the argument you gave it into a boolean primitive value and returns the result. (See §15.6.1 in the spec.)

  2. f is the name of the argument in the noisy function.

Functions in JavaScript are first-class objects. You can pass them into other functions as arguments just like any other object.

When you do

noisy(Boolean)(0)

There are two things going on. First:

// (In effect, we're not really creating a variable...)
var x = noisy(Boolean);

That gives us a function that, when called, will call Boolean with the argument we give it while also doing those console.log statements. This is the function you see being created in noisy (return function(arg)...);

Then we call that function:

x(0);

And that's when you see the console output. Since Boolean(0) is false, you see Boolean return that value.

Here's a much simpler example:

function foo(bar) {
    bar();
}
function testing() {
    alert("testing got called");
}
foo(testing);

There, I'm passing the function testing into foo. The argument name I'm using for that within foo is bar. The line bar(); calls the function.

Wednesday, June 2, 2021
 
Nil
answered 5 Months ago
Nil
62

ECMAScript 6

let filtered = arr.filter(dataRow => dataRow[2] === 'Red');

As noted by @ozeebee, ES6 is currently not supported in Google App Scripts, so you should try the following:

ECMAScript 5

var filtered = arr.filter(function (dataRow) {
  return dataRow[2] === 'Red';
});

In the comments, “classic way” refers to the ES5 method.

Explanation

.filter function takes a single parameter which is a callback to a function that returns true if array entry should remain or false if it should be removed, that’s the filtering. In this case, we should check whether third column of table row equals to Red. The code: return dataRow[2] === 'Red' is equal to:

if (dataRow[2] === 'Red') {
  return true;
} else {
  return false;
}

Because the result of comparison is a boolean.

See also

  • Array.prototype.filter at Mozilla Developer Network
Saturday, June 19, 2021
 
capsid
answered 5 Months ago
52

Filter the $result array using $old_keys, but use the $old_keys values as keys:

$old_keys = array_flip(['key1','key2','key3']);
$result = ['key1' => 'foo', 'key2' => 'bar', 'key6' => 'ipsum'];
$output=array_intersect_key($result,$old_keys);
print_r($output);

If you want to re-index the keys in the result, replace the $output declaration with:

$output=array_values(array_intersect_key($result,$old_keys));
Tuesday, August 31, 2021
 
MKM
answered 2 Months ago
MKM
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :