0

I have an associative PHP array and I would like to generate a list of keys that pass a certain test. For example

 $myArray = ('28'=>0.01,'51'=>-0.1,'48'=>0.4,'53'=>-0.3);

And I'd like to filter the keys in the same way I can simply filter the values. So if I filter the values on "return the elements that are bigger than 0.2" would be

 print_r(array_filter($myArray,"biggerThanFilter");

with

 function biggerThanFilter($v){
       return $v>0.2;
 }

But how would I apply a filter to the keys which say the "keyValueIsBiggerThan50"

i.e something like this

print_r(array_KEY_filter($myArray,"keyValueIsBiggerThan50");

function keyValueIsBiggerThan50($key){
         return $key*1>50;
}
nickL
  • 1,536
  • 2
  • 10
  • 15
  • You need to get only keys, without values? – hindmost Feb 04 '14 at 19:26
  • 1
    Duplicate of: http://stackoverflow.com/questions/4260086/php-how-to-use-array-filter-to-filter-array-keys – sigy Feb 04 '14 at 19:26
  • @sigy - yes I see(+1) and +1 for the person that answered that. But my question is clearer - the question in that post is more for matching rather than passing a test. – nickL Feb 04 '14 at 19:44

4 Answers4

2

I would loop through the array_keys and unset, personally:

function array_filter_keys($array, callable $fn)
  foreach (array_keys($array) as $key) {
    if (!$fn($key)) unset($array[$key])
  }
  return $array;
}

$filtered_array = array_filter_keys($array, function($key) { return $key > 50 });

This assumes PHP >= 5.4

dave
  • 62,300
  • 5
  • 72
  • 93
  • Thanks - the callable is what I was looking for as I can then use the function with similar syntax to the byValue functions. Thx. – nickL Feb 04 '14 at 19:40
1
function keyValueIsBiggerThan50 ($myArray) {
    $newArray = array();
    foreach($myArray as $key => $value){
        if($key * 1 > 50){
            $newArray[$key] = $value
        }
    }

    return $newArray;
}

to be used like

print_r(keyValueIsBiggerThan50 ($myArray));

Are you looking for this specific case, or a generic?

useSticks
  • 883
  • 7
  • 15
  • It was something more generic with a callback that I was after but I like your answer and I barely had a chance to buy a coffee before you'd posted... – nickL Feb 04 '14 at 19:39
0

For PHP 5.6+ my answer to a similar question also applies to this one: use ARRAY_FILTER_USE_KEY

<?php
$myArray  = ['28' => 0.01, '51' => -0.1, '48' => 0.4, '53' => -0.3];
$filtered = array_filter(
    $myArray,
    function ($key) {
        return $key > 50;
    },
    ARRAY_FILTER_USE_KEY
);
?>
Community
  • 1
  • 1
Richard Turner
  • 12,506
  • 6
  • 36
  • 37
0

For PHP < 5.6, array_diff_ukey performs a custom comparison of two arrays by key and appears to do a full N x M comparison so you can filter a single array by using a dummy as the second array.

Using PHP 5.5.9, I used the following to remove the numeric key elements from an array:

 $res = array_diff_ukey($res, array(0), function ($a,$b){ return is_string($a); });
sheddenizen
  • 506
  • 2
  • 11