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, requirements: Have the following two-dimensional array, how to remove the field of this array "on" to generate a new array $arr 2
$arr = Array ("net" = = Array ("On", "11", "101", "1001"),
"Login" = = Array ("On", "10001", "100001", "1000001"));

$arr 2 = Array ("net" = = Array ("11", "101", "1001"),
"Login" = = Array ("10001", "100001", "1000001"));
Or go through the time to remove on.
2, can achieve this effect can also
$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);
After the value operation, the number or operation gets the value $str, "On" this field is not able to participate in the operation



------to solve the idea----------------------
1, so remove
$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. Such arithmetic
$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
------to solve the idea----------------------
$v;
}
}
Var_dump ($STR);
String (7) "1111111"

------to solve the idea----------------------

$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
)

)
  • Related Article

    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.