php中怎么去除二维数组的某个字段?

WBOY
發布: 2016-06-23 13:41:54
原創
2344 人瀏覽過

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
)

)

好的,灰常感谢

2

$arr = array("net" => array("on","1", "2","4"), "login" => array("on","10001","100001","10000001"));delon($arr);$str ='';foreach ($arr as $key=>$value){    foreach ($value as $k =>$v){        $str=$str|$v;    }}var_dump($str); function delon(&$arr){    foreach($arr as $key=>&$val){        foreach($val as $k=>$v){            if($v=='on'){                unset($val[$k]);            }        }    }}
登入後複製



string(8) "70001101"

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板