假设我在php中有这样的数组:
$ray =
Array
(
[0] => Array
(
[id] => 1
[0] => 1
[ip] => 127.0.0.1
[1] => 127.0.0.1
[username] => local_me
[2] => local_me
[last_visit] => 2013-05-17 02:12:07
[3] => 2013-05-17 02:12:07
)
[1] => Array
(
[id] => 2
[0] => 2
[ip] => 127.2.2.3
[1] => 127.2.2.3
[username] => Anonymous42
[2] => Anonymous42
[last_visit] => 2013-05-16 16:07:46
[3] => 2013-05-16 16:07:46
)
)
我想删除此数组所有元素的ip
(和'1')“列”。我怎么能这样做?
谢谢!
答案 0 :(得分:4)
foreach ($ray as &$item)
unset($item['ip'],$item[1]);
答案 1 :(得分:1)
此问题在此之前被问过:PHP Remove key from associative array
从数组中删除键非常简单,只需使用 unset 方法。
foreach($ray as $key => $value){
if(array_key_exists('id', $value){
unset($ray[$key]['id'];
}
elseif(array_key_exists('1', $value){
unset($ray[$key]['1'];
}
}
当然,没有更多选择可以更有效地做到这一点,我只是试着举个例子。