简体   繁体   English

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

[英]How to filter an array by a part of key?

I have an array like this: 我有一个像这样的数组:

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;
}

and would like to filter by part of the key. 并希望按部分键进行过滤。 In this case I would like to have all keys that have "id-", and to result in this: 在这种情况下,我想拥有所有具有“ id-”的键,并导致以下结果:

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

Can anyone help? 有人可以帮忙吗?

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

this way you can do. 这样就可以做到。

$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

Answers, on the internet, that utilize array_intersect_key are INcorrect. 在互联网上,使用array_intersect_key的答案不正确。 They Filter Right, but Output "filtered keys index" values instead of input array values. 它们向右过滤,但是输出“已过滤键索引”值而不是输入数组值。

Here is a fixed version 这是固定版本

/**
 * 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;
}

If you're using the latest PHP version: 如果您使用最新的PHP版本:

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

function filter_my_array( $key ) {

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

}

Try with: 尝试:

$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;
  }
}

Use the following (untested): 使用以下内容(未经测试):

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);

You can write a little function that works analog to array_filter : 您可以编写一个类似于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)
    ;
}

prints 版画

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM