array("on", "11", "101", "1001"), &nbs"/>  array("on", "11", "101", "1001"), &nbs">

Home >Backend Development >PHP Tutorial >php中如何去除二维数组的某个字段

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

WBOY
WBOYOriginal
2016-06-13 12:13:262818browse

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(<br />  "net" => array("on", "11", "101", "1001"),<br />  "login" => array("on", "10001","100001","1000001")<br />);<br /><br />array_walk($arr, function(&$t) {<br />  $t = array_diff($t, array('on'));<br />});<br /><br />print_r($arr);
Array<br />(<br />    [net] => Array<br />        (<br />            [1] => 11<br />            [2] => 101<br />            [3] => 1001<br />        )<br /><br />    [login] => Array<br />        (<br />            [1] => 10001<br />            [2] => 100001<br />            [3] => 1000001<br />        )<br /><br />)<br />


2、这样运算
$arr = array(<br />  "net" => array("on", "11", "101", "1001"),<br />  "login" => array("on", "10001","100001","1000001")<br />);<br /><br />$str ='';<br />foreach ($arr as $key=>$value){<br />    foreach ($value as $k =>$v){<br />        if($v != 'on') $str = $str<br><font color='#FF8000'>------解决思路----------------------</font><br>$v;<br />    }<br />}<br />var_dump($str);
string(7) "1111111"<br />

------解决思路----------------------
<br />$arr = array("net" => array("on", "11", "101", "1001"),<br />    "login" => array("on", "10001","100001","1000001"));<br /><br />delon($arr);<br />print_r($arr);<br /><br /><br />function delon(&$arr){<br />	foreach($arr as $key=>&$val){<br />		foreach($val as $k=>$v){<br />			if($v=='on'){<br />				unset($val[$k]);<br />			}<br />		}<br />	}<br />}<br />


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

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

)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn