How to remove a field from a two-dimensional array in php

Source: Internet
Author: User
How to remove a field from a two-dimensional array in php? 1. requirement: There are two-dimensional arrays. how to remove the field on & nbsp; of this array to generate a new array & nbsp; $ arr2 $ arr & nbsp; array (net & nbsp; & gt; & nbsp; array (on, & nbsp; 11, & nbsp; 101, & nbsp; 1001), & nbsp; & how to remove a field from a two-dimensional array in nbs php?
1. requirement: There are two-dimensional arrays. how to remove the field "on" of this array to generate a new array $ 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 "));
You can also remove the on when traversing.
2. you can achieve this effect.
$ 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 );
// The Value $ str, "on" cannot be calculated based on the number or operation after the value operation.



------ Solution ----------------------
1. remove this
$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. this operation
$ 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
------ Solution ----------------------
$ V;
}
}
Var_dump ($ str );
string(7) "1111111"

------ Solution ----------------------

$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] = & gt; 1001
)

[Login] => Array
(
[1] => 10001.
[2] => 100001.
[3] = & gt; 1000001
)

)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.