【发布时间】:2015-12-26 22:45:26
【问题描述】:
我有类似这样的数组
$arr =
['0' =>
['0' => 'zero',
'1' => 'test',
'2' =>'testphp',
'test'=>'zero',
'test1'=>'test',
'test2'=>'testphp'],
'1' =>
['0' => 'z',
'1' => 'x',
'2' =>'c',
'test'=>'z',
'test1'=>'x',
'test2'=>'c']
];
并且 0,1,2 与 test,test1,test2 相同。我需要删除像 test、test1、test2 这样的字符串的键。 我知道路
foreach($arr as $a){
unset($arr['test']);
unset($arr['test1']);
unset($arr['test2']);
}
但可以在不指定确切名称的情况下找到键,因为我只想要数字键。
【问题讨论】:
-
$arr = array_filter($arr, function ($key) {return is_numeric($key); }, ARRAY_FILTER_USE_KEY );如果你运行的是 PHP >= 5.6.0 -
但是您是从数据库中获取这个数组吗?如果是这样,你可以告诉你的 fetch 只返回一个枚举数组