Tmtowtdi. Dưới đây là một số giải pháp theo thứ tự phức tạp.
. Câu chuyện toán học rời rạc dài, bạn chỉ thực sự phải lo lắng về trường hợp xấu nhất và đảm bảo đó không phải là n ^ 2
hay n!
. Đó là thước đo của sự thay đổi về thời gian tính toán khi n
tăng so với thời gian tính toán tổng thể. Wikipedia có một bài viết tốt về tính phức tạp thời gian tính toán.worst case scenario where n
means the number of elements in the array, and o(n)
or "little o" means best case scenario. Long discrete math story short, you only really have to worry about the worst
case scenario, and make sure it's not n ^ 2
or n!
. It's more a measure of change in computing time as n
increases than it is overall computing time. Wikipedia has a good article about computational aka time complexity.
Nếu kinh nghiệm đã dạy tôi bất cứ điều gì, thì đó là việc dành quá nhiều thời gian để tối ưu hóa ít chương trình của bạn là một sự lãng phí thời gian khác biệt tốt hơn để làm một cái gì đó - bất cứ điều gì - tốt hơn.
Giải pháp này có kịch bản trường hợp tốt nhất là 1 so sánh - 1 lần lặp qua vòng lặp, nhưng chỉ cung cấp giá trị phù hợp ở vị trí 0 của mảng. Trường hợp xấu nhất là nó không nằm trong mảng, và do đó phải lặp lại mọi yếu tố của mảng.only provided the matching value is in position 0 of the array. The worst case scenario is it's not in the array, and thus has to iterate over every element of the array.
Giải pháp này phải lặp qua toàn bộ mảng bất kể giá trị phù hợp ở đâu, do đó, nó sẽ luôn luôn là n
lặp qua mảng.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcBàn luậnXem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Iterating over the array and searching for significant match is the simplest approach one can follow. Check if an element of the given array is itself an array or not and add the element to the search path, else run array search on the nested array.
Example:
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
9Trong PHP, tìm kiếm mảng đa chiều đề cập đến việc tìm kiếm một giá trị trong một mảng lồng nhau đa cấp. Có nhiều kỹ thuật khác nhau để thực hiện loại tìm kiếm này, chẳng hạn như lặp lại trên các mảng lồng nhau, phương pháp đệ quy và các hàm tìm kiếm mảng sẵn có.
Cách tiếp cận lặp: Lặp lại trên mảng và tìm kiếm kết hợp đáng kể là cách tiếp cận đơn giản nhất mà người ta có thể làm theo. Kiểm tra xem một phần tử của mảng đã cho có phải là một mảng hay không và thêm phần tử vào đường dẫn tìm kiếm, khác chạy tìm kiếm mảng trên mảng lồng nhau.
$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
0 $existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
1$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
2223____$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
4$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
3$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
6$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
7$ --> 1 --> subject
7$ --> school3 --> data --> name
3$ --> 1 --> subject
0$ --> 1 --> subject
8$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
3$ --> 1 --> subject
3$ --> school3 --> data --> name
8$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
9 $ --> 1 --> subject
0$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
4 $ --> 1 --> subject
2 $ --> 1 --> subject
3 $ --> 1 --> subject
4$ --> 1 --> subject
5$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
7$ --> 1 --> subject
7$ --> 1 --> subject
8 $ --> 1 --> subject
9$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
6$ --> school3 --> data --> name
1Các
o(n)
7
$ --> school3 --> data --> name
3$ --> 1 --> subject
0$ --> 1 --> subject
8$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
3n
6$ --> school3 --> data --> name
8n
1
$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
9 $ --> 1 --> subject
0$ --> 1 --> subject
5 $ --> 1 --> subject
2 n
6 $ --> 1 --> subject
4n
8__o(n)
0n!
2
n
1n!
2
$ --> 1 --> subject
7n!
2o(n)
0O(n)
0
$ --> 1 --> subject
0n
8 o(n)
4$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
2227o(n)
7n ^ 2
5 n ^ 2
6n ^ 2
7
$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
3$ --> 1 --> subject
8$ --> school3 --> data --> name
8$ --> 1 --> subject
7n!
2$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8n!
2$ --> 1 --> subject
7n!
8$ --> 1 --> subject
03035 o(n)
4$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
2227n!
2
n
1n ^ 2
5 n ^ 2
6n ^ 2
7
$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
3$ --> 1 --> subject
8$ --> school3 --> data --> name
8$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8n ^ 2
5 return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
17return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
19 $ --> 1 --> subject
9return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
27 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
29return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
41$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
32 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
34return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
37 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
39$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
27 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
48return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
41$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
32 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
53return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
37 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
58$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
37 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
77$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
79$ --> school3 --> data --> name
8$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
27 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
67return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30o(n)
7
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
19$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
3return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
90return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
91return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
92$ --> 1 --> subject
0return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
81$ --> school3 --> data --> name
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
96Output:
$ --> 1 --> subject
$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
32 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
72return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30
In case, when levels of nested arrays increase, it becomes hard to write such programs and debug them. In such cases its better to write a recursive program which can cleanly be written without
adding any nested for loops.
Example:
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
9return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
81 return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
82return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
58return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8O(n)
0$ --> 1 --> subject
0O(n)
2$ --> 1 --> subject
0$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
4$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
12O(n)
7$ --> 1 --> subject
0$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
4$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
16Cách tiếp cận đệ quy: Trong trường hợp, khi các cấp độ lồng nhau tăng lên, thật khó để viết các chương trình như vậy và gỡ lỗi chúng. Trong những trường hợp như vậy, tốt hơn là viết một chương trình đệ quy có thể được viết một cách sạch sẽ mà không cần thêm bất kỳ vòng lặp cho các vòng lặp.
n
1
$ --> 1 --> subject
8 $ --> 1 --> subject
9$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
6$ --> school3 --> data --> name
1n
1
$ --> school3 --> data --> name
3$ --> 1 --> subject
0$ --> 1 --> subject
8$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
3$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
22$ --> school3 --> data --> name
8Các
o(n)
0
$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
50 $existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
51$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
52$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
2$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
3$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
24$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
3$ --> 1 --> subject
8$ --> school3 --> data --> name
8o(n)
0O(n)
0
$ --> 1 --> subject
0$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
50 $existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
63o(n)
7n ^ 2
5
$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
50$ --> school3 --> data --> name
1o(n)
0n!
2
n
1n!
2
n
1
$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
73 O(n)
0$ --> 1 --> subject
0$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
24 o(n)
4$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
2227o(n)
0n ^ 2
5 n ^ 2
6n ^ 2
7
$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
3$ --> 1 --> subject
8$ --> school3 --> data --> name
8n
1n!
2
$ --> 1 --> subject
7n!
2$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8n!
2$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8n ^ 2
5 return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
17n!
2
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
19 $ --> 1 --> subject
9return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8$ --> 1 --> subject
02 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$ --> 1 --> subject
7$ --> 1 --> subject
07 $ --> 1 --> subject
4$ --> 1 --> subject
09return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30$ --> 1 --> subject
7$ --> 1 --> subject
12 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
27 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
29return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
32 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
34return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
37 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
39$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
79$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
41$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8$ --> 1 --> subject
35 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$ --> 1 --> subject
7$ --> 1 --> subject
07 $ --> 1 --> subject
4$ --> 1 --> subject
42return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30$ --> 1 --> subject
7$ --> 1 --> subject
12 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
27 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
29return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
32 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
34return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
37 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
39$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
79$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
41$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8$ --> 1 --> subject
35 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$ --> 1 --> subject
7$ --> 1 --> subject
07 $ --> 1 --> subject
4$ --> 1 --> subject
42return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30$ --> 1 --> subject
7$ --> 1 --> subject
12 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
27 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
29return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
32 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
34return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
37 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
39$ --> 1 --> subject
7return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
79$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
79$ --> school3 --> data --> name
8$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8$ --> 1 --> subject
35 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
92$ --> 1 --> subject
0return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
81$ --> school3 --> data --> name
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
96Output:
$ --> school3 --> data --> name
$ --> 1 --> subject
7$ --> 1 --> subject
07 $ --> 1 --> subject
4$ --> 1 --> subject
42return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30
The
array_search() is an inbuilt function which searches for a given value related to the given array column/key. This function only returns the key index instead of a search path. The array_column() function returns the values from a single column in the input array.
Example:
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
9return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
19 $ --> 1 --> subject
9return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8$ --> 1 --> subject
02 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$ --> 1 --> subject
7$ --> 1 --> subject
07 $ --> 1 --> subject
4$ --> 1 --> subject
09return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30$ --> 1 --> subject
7$ --> 1 --> subject
12 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
27 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
29return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
41$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
32 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
34return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
37 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
39$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8$ --> 1 --> subject
35 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
41$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0$ --> 1 --> subject
7$ --> 1 --> subject
07 $ --> 1 --> subject
4$ --> 1 --> subject
42return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
27 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
48return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
32 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
53return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
79$ --> school3 --> data --> name
8n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
37 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
58$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];
8$ --> 1 --> subject
68 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
21$ --> 1 --> subject
0return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
96n
1
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
27 $ --> 1 --> subject
4return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
67return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
30
Tìm kiếm mảng đa chiều bằng phương thức Array_Search (): Array_Search () là một hàm sẵn có tìm kiếm một giá trị đã cho liên quan đến cột/khóa mảng đã cho. Hàm này chỉ trả về chỉ mục chính thay vì đường dẫn tìm kiếm.array_search() method:
The array_search() is an inbuilt function which searches for a given value related to the given array column/key. This function only returns the key index instead of a search path.
Hàm in_array () là một hàm sẵn có trong PHP được sử dụng để kiểm tra xem một giá trị nhất định có tồn tại trong một mảng hay không.Nó trả về đúng nếu giá trị đã cho được tìm thấy trong mảng đã cho và sai.in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.