我在php
中有多维数组我希望从每个数组中删除一个索引,我的数组是这样的
Array
(
[0] => Array
(
[ProfilePrivacySettingDefaultID] => 1
[UserType] => 2
[Type] => 1
[EntityID] => 0
[PrivacySettingPublic] => 1
[PrivacySettingElephantiUser] => 1
[PrivacySettingFriend] => 1
[IsValid] => 1
[CreatedOn] => 1330580809
[CreatedBy] => 0
[UpdatedOn] => 0
[UpdatedBy] => 0
)
[1] => Array
(
[ProfilePrivacySettingDefaultID] => 2
[UserType] => 2
[Type] => 1
[EntityID] => 0
[PrivacySettingPublic] => 1
[PrivacySettingElephantiUser] => 1
[PrivacySettingFriend] => 1
[IsValid] => 1
[CreatedOn] => 1330580809
[CreatedBy] => 0
[UpdatedOn] => 0
[UpdatedBy] => 0
)
[2] => Array
(
[ProfilePrivacySettingDefaultID] => 3
[UserType] => 2
[Type] => 1
[EntityID] => 0
[PrivacySettingPublic] => 1
[PrivacySettingElephantiUser] => 1
[PrivacySettingFriend] => 1
[IsValid] => 1
[CreatedOn] => 1330580809
[CreatedBy] => 0
[UpdatedOn] => 0
[UpdatedBy] => 0
)
[3] => Array
(
[ProfilePrivacySettingDefaultID] => 4
[UserType] => 2
[Type] => 1
[EntityID] => 0
[PrivacySettingPublic] => 1
[PrivacySettingElephantiUser] => 1
[PrivacySettingFriend] => 1
[IsValid] => 1
[CreatedOn] => 1330580809
[CreatedBy] => 0
[UpdatedOn] => 0
[UpdatedBy] => 0
)
);
我希望所有数组都有索引ProfilePrivacySettingDefaultID
,因此最终结果将是这样的
Array
(
[0] => Array
(
[UserType] => 2
[Type] => 1
[EntityID] => 0
[PrivacySettingPublic] => 1
[PrivacySettingElephantiUser] => 1
[PrivacySettingFriend] => 1
[IsValid] => 1
[CreatedOn] => 1330580809
[CreatedBy] => 0
[UpdatedOn] => 0
[UpdatedBy] => 0
)
[1] => Array
(
[UserType] => 2
[Type] => 1
[EntityID] => 0
[PrivacySettingPublic] => 1
[PrivacySettingElephantiUser] => 1
[PrivacySettingFriend] => 1
[IsValid] => 1
[CreatedOn] => 1330580809
[CreatedBy] => 0
[UpdatedOn] => 0
[UpdatedBy] => 0
)
[2] => Array
(
[UserType] => 2
[Type] => 1
[EntityID] => 0
[PrivacySettingPublic] => 1
[PrivacySettingElephantiUser] => 1
[PrivacySettingFriend] => 1
[IsValid] => 1
[CreatedOn] => 1330580809
[CreatedBy] => 0
[UpdatedOn] => 0
[UpdatedBy] => 0
)
[3] => Array
(
[UserType] => 2
[Type] => 1
[EntityID] => 0
[PrivacySettingPublic] => 1
[PrivacySettingElephantiUser] => 1
[PrivacySettingFriend] => 1
[IsValid] => 1
[CreatedOn] => 1330580809
[CreatedBy] => 0
[UpdatedOn] => 0
[UpdatedBy] => 0
)
);
最好的方法是什么,非常感谢..............
答案 0 :(得分:3)
您可以轻松使用array_map
执行此操作,并使用unset
删除索引。例如:
$your_array = array_map(
function($array) {
unset($array['ProfilePrivacySettingDefaultID']);
},
$your_array
);
答案 1 :(得分:0)
我想说foreach将是最有效的方法。
所以
foreach($user_array as $key => $value){
unset($user_array[$key]['ProfilePrivacySettingDefaultID']);
}
或
foreach($user_array as &$value){
unset($value['ProfilePrivacySettingDefaultID']);
}
array_map只会比需要做更多的工作并且使用匿名函数,因为你的array_map回调只是适得其反。
$user_array = array_map(
function($value){
unset($value['ProfilePrivacySettingDefaultID']);
return $value;
}, $user_array
);
function array_map ( $callback , $array){
foreach($array as $key => &$value){
$value = call_user_func( $callback, $value );
// which is equal to
/*function my_callback($value){
unset($value['ProfilePrivacySettingDefaultID']);
return $value;
}
$value = my_callback();*/
}
return $array;
}
答案 2 :(得分:-1)
我只是迭代数组并取消设置特定值:
foreach($array AS $value){
unset($value['ProfilePrivacySettingDefaultID']);
}