我有一个关联的Php数组,在print_r
上看起来像这样 Array ( [dyna[0]] => one [add[0]] => ⇊ [rem[0]] => Delete Column [dyna[1]] => two [add[1]] => ⇊ [rem[1]] => Delete Column [dyna[2]] => three [add[2]] => ⇊ [rem[2]] => Delete Column [dyna[3]] => four [add[3]] => ⇊ [rem[3]] => Delete Column [input[1]] => numbers [store_last] => 6 )
问:我想只保留数组中包含密钥的元素 - > dyna [0],dyna [1]并删除剩余的键,其中包含类似于添加[0]和rem [1]等键的值。
有可能吗?请建议
感谢。
答案 0 :(得分:1)
poor允许基于用户功能轻松进行阵列过滤:
$oldArray['dyna[0]']='a';
$oldArray['zzzzzzzzzzzzzz']='b';
$oldArray['dyna[1]']='c';
$oldArray['zzzzzzzzzzzz']='d';
print_r($oldArray);
$newArray = array_filter(
$oldArray,
function ($key) {
return preg_match('/^dyna\[\d+\]/', $key); //regular expression to match key name
},
ARRAY_FILTER_USE_KEY
);
print_r($newArray);
//输出:
Array
(
[dyna[0]] => a
[zzzzzzzzzzzzzz] => b
[dyna[1]] => c
[zzzzzzzzzzzz] => d
)
Array
(
[dyna[0]] => a
[dyna[1]] => c
)