我正在尝试抓取任何具有“release _”键的数据......我想我应该使用array_filter,但是我很难为param 2编写正确的函数。
这是我的阵列:
Array
(
[release_date] => Array
(
[type] => date
[value] => 1999-03-31
)
[duration] => Array
(
[type] => time
[value] => 136
)
[directors] => Array
(
[type] => page
[value] => 13,14
)
[writers] => Array
(
[type] => page
[value] => 13,14
)
[release_us] => Array
(
[type] => date
[value] => 1999-03-30
)
[release_ge] => Array
(
[type] => date
[value] => 1999-06-16
)
)
我希望我的返回数组只有release_存在的数组..所以它看起来像:
Array
(
[release_date] => Array
(
[type] => date
[value] => 1999-03-31
)
[release_us] => Array
(
[type] => date
[value] => 1999-03-30
)
[release_ge] => Array
(
[type] => date
[value] => 1999-06-16
)
)
答案 0 :(得分:2)
function preg_grep_keys( $pattern, $input, $flags = 0 )
{
$keys = preg_grep( $pattern, array_keys( $input ), $flags );
$vals = array();
foreach ( $keys as $key )
{
$vals[$key] = $input[$key];
}
return $vals;
}
用法:
preg_grep_keys("/release_/", $yourArray);
答案 1 :(得分:1)
只需创建foreach
即可遍历原始数组:
$nArr = array();
foreach($arr as $key => $value) {
if(strpos($key, 'release_') !== false) {
$nArr[$key] = $value;
}
}
修改强>
或以功能形式:
function checkKeys($arr, $keyCheck) {
$nArr = array();
foreach($arr as $key => $value) {
if(strpos($key, $keyCheck) !== false) {
$nArr[$key] = $value;
}
}
return $nArr;
}
然后使用$release = checkKeys($myArray, 'release_');