如何通过密钥的一部分过滤数组?

时间:2012-02-10 12:29:55

标签: php arrays

我有一个这样的数组:

Array(
[id-1] => N
[nm-1] => my_val_01;
[id-48] => S
[nm-48] => my_val_02;
[id-52] => N
[nm-52] => my_val_03;
[id-49] => N
[nm-49] => my_val_04;
[id-50] => N
[nm-50] => my_val_05;
}

并希望过滤部分密钥。在这种情况下,我希望所有密钥都具有“id-”,并导致:

Array(
[id-1] => N
[id-48] => S
[id-52] => N
[id-49] => N
[id-50] => N
}

有人可以帮忙吗?

7 个答案:

答案 0 :(得分:3)

foreach (array_keys($array) as $key) {
    if (!preg_match('/^id-\d+/', $key)) {
        unset($array[$key]);
    }
}

答案 1 :(得分:1)

这样你就可以做到。

$arr = Array(
[id-1] => N
[nm-1] => my_val_01;
[id-48] => S
[nm-48] => my_val_02;
[id-52] => N
[nm-52] => my_val_03;
[id-49] => N
[nm-49] => my_val_04;
[id-50] => N
[nm-50] => my_val_05;
};
$new = array();
foreach($arr as $key => $value){
  if(stripos($key, "id-") !== false){
   $new[$key] = $value;
  }
}
//so $new is your required array here

答案 2 :(得分:0)

尝试:

$input  = array( /* your data */ );
$output = array();
$pattern = 'id-';

foreach ( $input as $key => $value ) {
  if ( strpos($key, $pattern) === 0 ) { # Use identical not equal operator here
    $output[$key] = $value;
  }
}

答案 3 :(得分:0)

使用以下(未经测试):

function filter($key) {
    return substr($key, 0, 3) == 'id-';
}

$keys = array_filter(array_keys($array), 'filter');
if(empty($keys))
    $array = array();
else
    $array = array_intersect_key(array_flip($keys), $input);

答案 4 :(得分:0)

您可以编写一个与array_filter类似的小函数:

<?php
$a = array(
    'id-1' => 'N', 'nm-1' => 'my_val_01', 'id-48' => 'S', 'nm-48' => 'my_val_02', 'id-52' => 'N',
    'nm-52' => 'my_val_03', 'id-49' => 'N', 'nm-49' => 'my_val_04', 'id-50' => 'N', 'nm-50' => 'my_val_05'
);

$b = array_filter_key($a, function($e) {
    return 0===strpos($e, 'id-');
});
var_dump($b);




function array_filter_key($source, $callback)   {
    // TODO: check arguments
    $keys = array_keys($source);
    $keys = array_filter($keys, $callback);

    return ( 0===count($keys) ) ?
        array()
        :
        array_intersect_key(array_flip($keys), $source)
    ;
}

打印

array(5) {
  ["id-1"]=>
  int(0)
  ["id-48"]=>
  int(2)
  ["id-52"]=>
  int(4)
  ["id-49"]=>
  int(6)
  ["id-50"]=>
  int(8)
}

答案 5 :(得分:0)

互联网上使用array_intersect_key的答案是不正确的。 它们过滤右,但输出“过滤键索引”值而不是输入数组值。

这是一个固定版本

/**
 * Filter array keys by callback
 * 
 * @param array $input
 * @param $callback
 * @return NULL|unknown|multitype:
 */
function array_filter_keys($input, $callback)
{
    if (!is_array($input)) {
        trigger_error('array_filter_key() expects parameter 1 to be array, ' . gettype($input) . ' given', E_USER_WARNING);
        return null;
    }

    if (empty($input)) {
        return $input;
    }

    $filteredKeys = array_filter(array_keys($input), $callback);
    if (empty($filteredKeys)) {
        return array();
    }

    $input = array_intersect_key($input, array_flip($filteredKeys));

    return $input;
}

答案 6 :(得分:0)

如果您使用的是最新的PHP版本:

$filtered_arr = array_filter($arr, 'filter_my_array', ARRAY_FILTER_USE_KEY);

function filter_my_array( $key ) {

    return preg_match("/^id-\d+/", $key);

}