Bạn bằng cách nào đó phải lặp qua mảng của bạn và lọc từng phần tử theo điều kiện của bạn. Điều này có thể được thực hiện với các phương pháp khác nhau.
Vòng lặp while
/ for
/ //Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
0 Phương pháp
//Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
Vòng lặp qua mảng của bạn với bất kỳ vòng lặp bạn muốn, có thể là while
, for
hoặc
//Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
0. Sau đó, chỉ cần kiểm tra tình trạng của bạn và //Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
4 Các yếu tố nếu chúng không đáp ứng điều kiện của bạn hoặc viết các yếu tố, đáp ứng điều kiện, thành một mảng mới.Vòng lặp
//while loop
while[list[$key, $value] = each[$array]]{
//condition
}
//for loop
$keys = array_keys[$array];
for[$counter = 0, $length = count[$array]; $counter < $length; $counter++]{
$key = $keys[$counter];
$value = $array[$key];
//condition
}
//foreach loop
foreach[$array as $key => $value]{
//condition
}
Tình trạng
Chỉ cần đặt điều kiện của bạn vào vòng lặp nơi nhận xét
//Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
5. Điều kiện chỉ có thể kiểm tra bất cứ điều gì bạn muốn và sau đó bạn có thể //Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
4 các yếu tố không đáp ứng điều kiện của bạn và tái xuất mảng với //Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
7 nếu bạn muốn hoặc viết các yếu tố trong một mảng mới đáp ứng điều kiện.//Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
Phương pháp //Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
8
//Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
Một phương pháp khác là sử dụng chức năng tích hợp
//Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
8. Nó thường hoạt động khá giống với phương pháp với một vòng lặp đơn giản.Bạn chỉ cần trả lại
//Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
0 nếu bạn muốn giữ phần tử trong mảng và //Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
1 nếu bạn muốn loại bỏ phần tử ra khỏi mảng kết quả.//Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
Phương pháp //Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
2
//Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
//Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
2 tương tự như //Pseudo code
//Use one of the two ways
if[condition]{ //1. Condition fulfilled
$newArray[ ] = $value;
//↑ Put '$key' there, if you want to keep the original keys
//Result array is: $newArray
} else { //2. Condition NOT fulfilled
unset[$array[$key]];
//Use array_values[] after the loop if you want to reindex the array
//Result array is: $array
}
8 chỉ là nó chỉ sử dụng biểu thức chính quy để lọc mảng. Vì vậy, bạn có thể không thể làm mọi thứ với nó, vì bạn chỉ có thể sử dụng biểu thức thông thường làm bộ lọc và bạn chỉ có thể lọc theo các giá trị hoặc với một số mã khác bằng các khóa.Cũng lưu ý rằng bạn có thể chuyển cờ
//Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
5 dưới dạng tham số thứ ba để đảo ngược kết quả.//Filter by values
$newArray = preg_grep["/regex/", $array];
Điều kiện phổ biến
Có nhiều điều kiện phổ biến được sử dụng để lọc một mảng trong đó tất cả có thể được áp dụng cho giá trị và khóa của mảng. Tôi sẽ chỉ liệt kê một vài trong số họ ở đây:
//Odd values
return $value & 1;
//Even values
return ![$value & 1];
//NOT null values
return !is_null[$value];
//NOT 0 values
return $value !== 0;
//Contain certain value values
return strpos[$value, $needle] !== FALSE; //Use 'use[$needle]' to get the var into scope
//Contain certain substring at position values
return substr[$value, $position, $length] === $subString;
//NOT 'empty'[link] values
array_filter[$array]; //Leave out the callback parameter
[Php 4> = 4.0.6, Php 5, Php 7, Php 8]
Array_Filter - Lọc các thành phần của một mảng bằng hàm gọi lại — Filters elements of an array using a callback function
Sự mô tả
mảng_filter [mảng
6,? có thể gọi //Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
7 = //Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
8, int //Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
9 = 0]: mảng[array //Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
6,
?callable //Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
7 = //Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
8, int //Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
9 = 0]: array//Anonymous function
$newArray = array_filter[$array, function[$value, $key]{
//condition
}, ARRAY_FILTER_USE_BOTH];
//Function name passed as string
function filter[$value, $key]{
//condition
}
$newArray = array_filter[$array, "filter", ARRAY_FILTER_USE_BOTH];
//'create_function[]', NOT recommended
$newArray = array_filter[$array, create_function['$value, $key', '/* condition */'], ARRAY_FILTER_USE_BOTH];
Các phím mảng được bảo tồn và có thể dẫn đến các khoảng trống nếu
//Filter by values
$newArray = preg_grep["/regex/", $array];
0 được lập chỉ mục. Mảng kết quả có thể được tái phát bằng hàm mảng_Values [].array can be reindexed using the
array_values[] function. Thông số
//Filter by values
$newArray = preg_grep["/regex/", $array];
0Mảng để lặp lại
//Filter by values
$newArray = preg_grep["/regex/", $array];
2Chức năng gọi lại để sử dụng
Nếu không có
//Filter by values
$newArray = preg_grep["/regex/", $array];
2 được cung cấp, tất cả các mục trống của //Filter by values
$newArray = preg_grep["/regex/", $array];
0 sẽ bị xóa. Xem trống [] để biết cách PHP định nghĩa trống trong trường hợp này.empty[] for how PHP defines empty in this case. //Filter by values
$newArray = preg_grep["/regex/", $array];
5Cờ xác định những đối số nào được gửi đến
//Filter by values
$newArray = preg_grep["/regex/", $array];
2:-
7 - Pass Key làm đối số duy nhất cho//Filter by values $newArray = preg_grep["/regex/", $array];
2 thay vì giá trị - pass key as the only argument to//Filter by values $newArray = preg_grep["/regex/", $array];
2 instead of the value//Filter by values $newArray = preg_grep["/regex/", $array];
-
9 - Chuyển cả giá trị và khóa làm đối số cho//Filter by values $newArray = preg_grep["/regex/", $array];
2 thay vì giá trị - pass both value and key as arguments to//Filter by values $newArray = preg_grep["/regex/", $array];
2 instead of the value//Filter by values $newArray = preg_grep["/regex/", $array];
//Odd values
return $value & 1;
//Even values
return ![$value & 1];
//NOT null values
return !is_null[$value];
//NOT 0 values
return $value !== 0;
//Contain certain value values
return strpos[$value, $needle] !== FALSE; //Use 'use[$needle]' to get the var into scope
//Contain certain substring at position values
return substr[$value, $position, $length] === $subString;
//NOT 'empty'[link] values
array_filter[$array]; //Leave out the callback parameter
1 sẽ chuyển giá trị làm đối số duy nhất cho //Filter by values
$newArray = preg_grep["/regex/", $array];
2 thay thế.Trả về giá trị
Trả về mảng được lọc.
Thay đổi
8.0.0 | 2 bây giờ là vô hiệu. |
8.0.0 | Nếu 2 mong đợi một tham số được truyền qua tham chiếu, hàm này bây giờ sẽ phát ra một 5. 5.
|
Ví dụ
Ví dụ #1 Array_filter [] ví dụarray_filter[] example
//Odd values
return $value & 1;
//Even values
return ![$value & 1];
//NOT null values
return !is_null[$value];
//NOT 0 values
return $value !== 0;
//Contain certain value values
return strpos[$value, $needle] !== FALSE; //Use 'use[$needle]' to get the var into scope
//Contain certain substring at position values
return substr[$value, $position, $length] === $subString;
//NOT 'empty'[link] values
array_filter[$array]; //Leave out the callback parameter
6//Odd values
return $value & 1;
//Even values
return ![$value & 1];
//NOT null values
return !is_null[$value];
//NOT 0 values
return $value !== 0;
//Contain certain value values
return strpos[$value, $needle] !== FALSE; //Use 'use[$needle]' to get the var into scope
//Contain certain substring at position values
return substr[$value, $position, $length] === $subString;
//NOT 'empty'[link] values
array_filter[$array]; //Leave out the callback parameter
7//Odd values
return $value & 1;
//Even values
return ![$value & 1];
//NOT null values
return !is_null[$value];
//NOT 0 values
return $value !== 0;
//Contain certain value values
return strpos[$value, $needle] !== FALSE; //Use 'use[$needle]' to get the var into scope
//Contain certain substring at position values
return substr[$value, $position, $length] === $subString;
//NOT 'empty'[link] values
array_filter[$array]; //Leave out the callback parameter
8Ví dụ trên sẽ xuất ra:
Odd : Array [ [a] => 1 [c] => 3 [e] => 5 ] Even: Array [ [0] => 6 [2] => 8 [4] => 10 [6] => 12 ]
Ví dụ #2 mảng_filter [] mà không có
2array_filter[] without //Filter by values
$newArray = preg_grep["/regex/", $array];
2//Filter by values
$newArray = preg_grep["/regex/", $array];
Odd : Array [ [a] => 1 [c] => 3 [e] => 5 ] Even: Array [ [0] => 6 [2] => 8 [4] => 10 [6] => 12 ]0
Odd : Array [ [a] => 1 [c] => 3 [e] => 5 ] Even: Array [ [0] => 6 [2] => 8 [4] => 10 [6] => 12 ]1
Odd : Array [ [a] => 1 [c] => 3 [e] => 5 ] Even: Array [ [0] => 6 [2] => 8 [4] => 10 [6] => 12 ]2
Ví dụ trên sẽ xuất ra:
Array [ [0] => foo [2] => -1 ]
Ví dụ #2 mảng_filter [] mà không có
2array_filter[] with //Filter by values
$newArray = preg_grep["/regex/", $array];
5//Filter by values
$newArray = preg_grep["/regex/", $array];
Odd : Array [ [a] => 1 [c] => 3 [e] => 5 ] Even: Array [ [0] => 6 [2] => 8 [4] => 10 [6] => 12 ]0
Odd : Array [ [a] => 1 [c] => 3 [e] => 5 ] Even: Array [ [0] => 6 [2] => 8 [4] => 10 [6] => 12 ]5
Odd : Array [ [a] => 1 [c] => 3 [e] => 5 ] Even: Array [ [0] => 6 [2] => 8 [4] => 10 [6] => 12 ]6
Ví dụ trên sẽ xuất ra:
array[1] { ["b"]=> int[2] } array[2] { ["b"]=> int[2] ["d"]=> int[4] }
Ví dụ #2 mảng_filter [] mà không có //Filter by values
$newArray = preg_grep["/regex/", $array];
2
//Filter by values
$newArray = preg_grep["/regex/", $array];
Ví dụ #3 mảng_filter [] với
5//Filter by values
$newArray = preg_grep["/regex/", $array];
Ghi chú
Thận trọng
- Nếu mảng được thay đổi từ chức năng gọi lại [ví dụ: phần tử được thêm, đã xóa hoặc giải phóng], hành vi của hàm này không được xác định.
- Xem thêm
- Array_Intersect [] - Tính toán giao điểm của mảng
- Array_Map [] - Áp dụng cuộc gọi lại cho các phần tử của các mảng đã cho
mảng_Reduce [] - lặp lại giảm mảng thành một giá trị duy nhất bằng cách sử dụng hàm gọi lại ¶
Array_walk [] - Áp dụng chức năng do người dùng cung cấp cho mọi thành viên của một mảng
Odd : Array [ [a] => 1 [c] => 3 [e] => 5 ] Even: Array [ [0] => 6 [2] => 8 [4] => 10 [6] => 12 ]7
Ẩn danh ¶ ¶
9 năm trước
Odd : Array [ [a] => 1 [c] => 3 [e] => 5 ] Even: Array [ [0] => 6 [2] => 8 [4] => 10 [6] => 12 ]8
Odd : Array [ [a] => 1 [c] => 3 [e] => 5 ] Even: Array [ [0] => 6 [2] => 8 [4] => 10 [6] => 12 ]9
Array [ [0] => foo [2] => -1 ]0
Array [ [0] => foo [2] => -1 ]1
Array [ [0] => foo [2] => -1 ]2
Array [ [0] => foo [2] => -1 ]3
nicolaj dot knudsen tại gmail dot com ¶ ¶
5 năm trước
Array [ [0] => foo [2] => -1 ]4
Array [ [0] => foo [2] => -1 ]5
Array [ [0] => foo [2] => -1 ]6
Array [ [0] => foo [2] => -1 ]7
Array [ [0] => foo [2] => -1 ]8
Array [ [0] => foo [2] => -1 ]3
Aschmidt tại Anamera Dot Net ¶
1 năm trước
array[1] { ["b"]=> int[2] } array[2] { ["b"]=> int[2] ["d"]=> int[4] }0
array[1] { ["b"]=> int[2] } array[2] { ["b"]=> int[2] ["d"]=> int[4] }1
array[1] { ["b"]=> int[2] } array[2] { ["b"]=> int[2] ["d"]=> int[4] }2
Array [ [0] => foo [2] => -1 ]3
Technyquist ¶ ¶
5 tháng trước
array[1] { ["b"]=> int[2] } array[2] { ["b"]=> int[2] ["d"]=> int[4] }4
array[1] { ["b"]=> int[2] } array[2] { ["b"]=> int[2] ["d"]=> int[4] }5
array[1] { ["b"]=> int[2] } array[2] { ["b"]=> int[2] ["d"]=> int[4] }6
array[1] { ["b"]=> int[2] } array[2] { ["b"]=> int[2] ["d"]=> int[4] }7
array[1] { ["b"]=> int[2] } array[2] { ["b"]=> int[2] ["d"]=> int[4] }8
array[1] { ["b"]=> int[2] } array[2] { ["b"]=> int[2] ["d"]=> int[4] }9
while
0
while
1
while
2
while
3
Array [ [0] => foo [2] => -1 ]3
Marc dot vanwoerkom tại Fernuni-Hagen Dot de ¶ ¶
18 năm trước
while
5
while
6
Array [ [0] => foo [2] => -1 ]3
Marc Dot Grey tại Gmail Dot Com ¶ ¶
8 năm trước
while
8
while
9
for
0
for
1
Array [ [0] => foo [2] => -1 ]3
Niko E ¶ ¶
1 tháng trước
for
3
for
4
for
5
for
6
for
7