Hướng dẫn check value in multidimensional array php - kiểm tra giá trị trong mảng đa chiều php

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 0: return 0 < count( array_filter( $my_array, function ($a) { return array_key_exists('id', $a) && $a['id'] == 152; } ) ); 1 Độ phức tạp:

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.

foreach ($my_array as $sub_array) {
    if (@$sub_array['id'] === 152) {
        return true;
    }
}
return false;

Giải pháp 1: return 0 < count( array_filter( $my_array, function ($a) { return array_key_exists('id', $a) && $a['id'] == 152; } ) ); 2 Độ phức tạp:

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.

return 0 < count(
    array_filter(
        $my_array,
        function ($a) {
            return array_key_exists('id', $a) && $a['id'] == 152;
        }
    )
);

Giải pháp 2: return 0 < count( array_filter( $my_array, function ($a) { return array_key_exists('id', $a) && $a['id'] == 152; } ) ); 4 Độ phức tạp:

Một chèn băm là nơi

return 0 < count(
    array_filter(
        $my_array,
        function ($a) {
            return array_key_exists('id', $a) && $a['id'] == 152;
        }
    )
);
5 đến từ; n Chèn băm =
return 0 < count(
    array_filter(
        $my_array,
        function ($a) {
            return array_key_exists('id', $a) && $a['id'] == 152;
        }
    )
);
7. Có một cảnh sát băm ở phần cuối là một
return 0 < count(
    array_filter(
        $my_array,
        function ($a) {
            return array_key_exists('id', $a) && $a['id'] == 152;
        }
    )
);
5 khác nhưng nó không được bao gồm bởi vì đó chỉ là cách thức toán học rời rạc.

$existence_hash = [];
foreach ($my_array as $sub_array) {
    $existence_hash[$sub_array['id']] = true;
}
return @$existence_hash['152'];

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn 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;
            }
        )
    );
    
    9

    Trong 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
    
    1

    Cá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'];
    
    3n6
    $ --> school3 --> data --> name
    
    8

    n1

    $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 n6
    $ --> 1 --> subject
    
    4n8__

    o(n)0n!2

    n1n!2

    $ --> 1 --> subject
    
    7n!2

    o(n)0O(n)0

    $ --> 1 --> subject
    
    0n8 o(n)4
    $existence_hash = [];
    foreach ($my_array as $sub_array) {
        $existence_hash[$sub_array['id']] = true;
    }
    return @$existence_hash['152'];
    
    2227

    o(n)7n ^ 25 n ^ 26n ^ 27

    $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'];
    
    2227

    n!2

    n1n ^ 25 n ^ 26n ^ 27

    $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'];
    
    8
    return 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'];
    
    8n ^ 25
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    17

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    19
    $ --> 1 --> subject
    
    9
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    21
    $ --> 1 --> subject
    
    0

    $ --> 1 --> subject
    
    7
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    27
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    29
    return 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'];
    
    8
    return 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
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    21
    $ --> 1 --> subject
    
    0

    $ --> 1 --> subject
    
    7
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    32
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    34
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    $ --> 1 --> subject
    
    7
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    37
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    39

    $ --> 1 --> subject
    
    7
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    27
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    48
    return 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'];
    
    8
    return 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
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    21
    $ --> 1 --> subject
    
    0

    $ --> 1 --> subject
    
    7
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    32
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    53
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    $ --> 1 --> subject
    
    7
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    37
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    58

    $ --> 1 --> subject
    
    7
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    37
    $ --> 1 --> subject
    
    4
    return 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'];
    
    8
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    79

    $ --> school3 --> data --> name
    
    8

    $ --> 1 --> subject
    
    7
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    27
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    67
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    o(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'];
    
    3
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    21
    $ --> 1 --> subject
    
    0
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    90
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    91

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    92
    $ --> 1 --> subject
    
    0
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    81
    $ --> school3 --> data --> name
    
    8

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    96

    Output:

    $ --> 1 --> subject
    

    $ --> 1 --> subject
    
    7
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    32
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    72
    return 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;
            }
        )
    );
    
    81
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    82
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    58
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    Cá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.

    $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'];
    
    16

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    9

    Is

    n1

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

    n1O(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'];
    
    24
    $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'];
    
    24
    $existence_hash = [];
    foreach ($my_array as $sub_array) {
        $existence_hash[$sub_array['id']] = true;
    }
    return @$existence_hash['152'];
    
    16

    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

    Các

    o(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'];
    
    63

    o(n)7n ^ 25

    $existence_hash = [];
    foreach ($my_array as $sub_array) {
        $existence_hash[$sub_array['id']] = true;
    }
    return @$existence_hash['152'];
    
    50
    $ --> school3 --> data --> name
    
    1

    o(n)0n!2

    n1n!2

    n1

    $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'];
    
    2227

    o(n)0n ^ 25 n ^ 26n ^ 27

    $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

    n1n!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 ^ 25
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    17

    n!2

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    19
    $ --> 1 --> subject
    
    9
    return 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
    
    4
    return 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
    
    09
    return 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
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    21
    $ --> 1 --> subject
    
    0

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    27
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    29
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    32
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    34
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    37
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    39

    $ --> 1 --> subject
    
    7
    return 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'];
    
    8
    return 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
    
    4
    return 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
    
    42
    return 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
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    21
    $ --> 1 --> subject
    
    0

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    27
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    29
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    32
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    34
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    37
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    39

    $ --> 1 --> subject
    
    7
    return 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'];
    
    8
    return 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
    
    4
    return 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
    
    42
    return 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
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    21
    $ --> 1 --> subject
    
    0

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    27
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    29
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    32
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    34
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    37
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    39

    $ --> 1 --> subject
    
    7
    return 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'];
    
    8
    return 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
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    21
    $ --> 1 --> subject
    
    0

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    92
    $ --> 1 --> subject
    
    0
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    81
    $ --> school3 --> data --> name
    
    8

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    96

    Output:

    $ --> school3 --> data --> name
    

    $ --> 1 --> subject
    
    7
    $ --> 1 --> subject
    
    07
    $ --> 1 --> subject
    
    4
    $ --> 1 --> subject
    
    42
    return 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;
            }
        )
    );
    
    9

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    19
    $ --> 1 --> subject
    
    9
    return 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
    
    4
    return 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
    
    09
    return 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
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    21
    $ --> 1 --> subject
    
    0

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    27
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    29
    return 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'];
    
    8
    return 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
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    21
    $ --> 1 --> subject
    
    0

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    32
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    34
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    37
    $ --> 1 --> subject
    
    4
    return 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
    
    4
    return 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
    return 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
    return 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
    
    42
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    27
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    48
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    32
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    53
    return 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'];
    
    8
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    79

    $ --> school3 --> data --> name
    
    8

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    37
    $ --> 1 --> subject
    
    4
    return 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
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    21
    $ --> 1 --> subject
    
    0

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    96

    n1

    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    27
    $ --> 1 --> subject
    
    4
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    67
    return 0 < count(
        array_filter(
            $my_array,
            function ($a) {
                return array_key_exists('id', $a) && $a['id'] == 152;
            }
        )
    );
    
    30


    Làm thế nào để bạn kiểm tra xem một giá trị tồn tại trong một mảng đa chiều?

    Hàm php isset () hàm isset () kiểm tra xem một biến được đặt, điều đó có nghĩa là nó phải được khai báo và không phải là null. Hàm này trả về đúng nếu biến tồn tại và không phải là null, nếu không nó sẽ trả về sai. The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.

    Làm thế nào để bạn kiểm tra giá trị chính tồn tại trong mảng đa chiều trong PHP?

    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.

    Làm thế nào để tìm kiếm giá trị trong mảng đa chiều trong PHP?

    Mã đơn giản để tìm kiếm giá trị trong mảng đa chiều được mô tả như sau: Array_Search ($ value ['id'], Array_column ($ studentaddress, 'user_id')))array_search($value['id'], array_column($studentsAddress, 'user_id'))

    Làm thế nào để bạn kiểm tra xem giá trị có tồn tại trong một mảng PHP không?

    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.