Hướng dẫn array filter with condition php - bộ lọc mảng với điều kiện php

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.

Show

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

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

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

//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ạiFilters elements of an array using a callback function

Sự mô tả

mảng_filter (mảng

//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,? 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

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);
0

Mảng để lặp lại

//Filter by values
$newArray = preg_grep("/regex/", $array);
2

Chứ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);
5

Cờ xác định những đối số nào được gửi đến

//Filter by values
$newArray = preg_grep("/regex/", $array);
2:

  • //Filter by values
    $newArray = preg_grep("/regex/", $array);
    
    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
Mặc định là
//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

Phiên bảnSự mô tả
8.0.0
//Filter by values
$newArray = preg_grep("/regex/", $array);
2 bây giờ là vô hiệu.
8.0.0 Nếu
//Filter by values
$newArray = preg_grep("/regex/", $array);
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
//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
5.
//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
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
8

Ví 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ó

//Filter by values
$newArray = preg_grep("/regex/", $array);
2array_filter() without
//Filter by values
$newArray = preg_grep("/regex/", $array);
2

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ó

//Filter by values
$newArray = preg_grep("/regex/", $array);
2array_filter() with
//Filter by values
$newArray = preg_grep("/regex/", $array);
5

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

Ví dụ #3 mảng_filter () với

//Filter by values
$newArray = preg_grep("/regex/", $array);
5

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

while0

while1

while2

while3

Array
(
    [0] => foo
    [2] => -1
)
3

Marc dot vanwoerkom tại Fernuni-Hagen Dot de ¶

18 năm trước

while5

while6

Array
(
    [0] => foo
    [2] => -1
)
3

Marc Dot Grey tại Gmail Dot Com ¶

8 năm trước

while8

while9

for0

for1

Array
(
    [0] => foo
    [2] => -1
)
3

Niko E ¶

1 tháng trước

for3

for4

for5

for6

for7

Làm thế nào để lọc giá trị mảng trong PHP?

Hàm mảng_filter () lọc các giá trị của một mảng bằng hàm gọi lại. Hàm này chuyển từng giá trị của mảng đầu vào đến hàm gọi lại. Nếu hàm gọi lại trả về true, giá trị hiện tại từ đầu vào được trả lại vào mảng kết quả. Các phím mảng được bảo tồn.. This function passes each value of the input array to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

Chức năng nào tạo ra một mảng dữ liệu được lọc vượt qua một điều kiện?

Người ta có thể sử dụng hàm Filter () trong JavaScript để lọc mảng đối tượng dựa trên các thuộc tính.Hàm bộ lọc () sẽ trả về một mảng mới chứa tất cả các phần tử mảng vượt qua điều kiện đã cho.filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition.

Làm thế nào để bạn lọc một mảng đa chiều?

Lọc mảng đa chiều theo khóa..
$ phpDevs = mảng_filter ($ mảng, hàm ($ key) {return $ key == 'phpDevelopers';}, mảng_filter_use_key);// Ngoài ra, hãy sử dụng cờ mảng_filter_use_both ..
$ phpDevs = mảng_filter ($ mảng, hàm ($ key) {return $ key == 'phpDevelopers';}) ;.

Làm thế nào kiểm tra mảng trống hoặc không trong PHP?

Sử dụng chức năng đếm: Hàm này đếm tất cả các phần tử trong một mảng.Nếu số lượng phần tử trong mảng bằng 0, thì nó sẽ hiển thị mảng trống.....
Sử dụng hàm sizeof (): Phương pháp này kiểm tra kích thước của mảng.Nếu kích thước của mảng bằng 0 thì mảng trống nếu không thì mảng không trống ..