欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php中如何去除二维数组的某个字段

程序员文章站 2022-06-05 15:10:11
...
php中怎么去除二维数组的某个字段?
1,需求:有如下二维数组,怎么去除此数组的字段"on" 生成一个新数组 $arr2
$arr = array("net" => array("on", "11", "101", "1001"),
"login" => array("on", "10001","100001","1000001"));

$arr2 = array("net" => array( "11", "101", "1001"),
"login" => array("10001","100001","1000001"));
或者遍历时候去掉on也行
2,能达到这样的效果也可以
$arr = array("net" => array("on","1", "2","4"),
"login" => array("on","10001","100001","10000001")
);
$str ='';
foreach ($arr as $key=>$value){
foreach ($value as $k =>$v){
$str=$str|$v;
}
}
var_dump($str);
//值运算后面数字的或运算得到值$str,"on"这个字段是不能参加运算的



------解决思路----------------------
1、这样去掉
$arr = array(
"net" => array("on", "11", "101", "1001"),
"login" => array("on", "10001","100001","1000001")
);

array_walk($arr, function(&$t) {
$t = array_diff($t, array('on'));
});

print_r($arr);
Array
(
[net] => Array
(
[1] => 11
[2] => 101
[3] => 1001
)

[login] => Array
(
[1] => 10001
[2] => 100001
[3] => 1000001
)

)


2、这样运算
$arr = array(
"net" => array("on", "11", "101", "1001"),
"login" => array("on", "10001","100001","1000001")
);

$str ='';
foreach ($arr as $key=>$value){
foreach ($value as $k =>$v){
if($v != 'on') $str = $str
------解决思路----------------------
$v;
}
}
var_dump($str);
string(7) "1111111"

------解决思路----------------------

$arr = array("net" => array("on", "11", "101", "1001"),
"login" => array("on", "10001","100001","1000001"));

delon($arr);
print_r($arr);


function delon(&$arr){
foreach($arr as $key=>&$val){
foreach($val as $k=>$v){
if($v=='on'){
unset($val[$k]);
}
}
}
}


Array
(
[net] => Array
(
[1] => 11
[2] => 101
[3] => 1001
)

[login] => Array
(
[1] => 10001
[2] => 100001
[3] => 1000001
)

)
php中如何去除二维数组的某个字段

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频