Summary of various methods for searching array elements in php

Source: Internet
Author: User

In php, data queries can be classified into one-dimensional array queries and multi-dimensional array queries. If it is a simple one-dimensional array, we can directly use in_array, array_search, and traversal for instances, if it is a multi-dimensional array, you need to use other methods.

The following operations can be performed on one-dimensional arrays:

The in_array 'function searches for the given value in the array. In_array (value, array, type) type is optional. If this parameter is set to true, check whether the data to be searched is of the same type as the value of the array.

The array_key_exists 'array _ key_exists () function checks whether a specified key exists in an array. If the key exists, true is returned. Otherwise, false is returned. Array_key_exists (key, array)

The array_search' array _ search () function is the same as the in_array () function. You can find a key value in the array. If this value is found, the key name of the matching element is returned. If not found, false is returned. Array_search (value, array, strict)

From this point of view, when the data volume is small, such as less than 1000, finding which row is used will not become a bottleneck;
When the data volume is large, it is more appropriate to use array_key_exists.
Of course, array_key_exists occupies a large amount of memory.

The binary method is used to check whether an array contains a certain element. It is compatible with positive and negative order. The code is implemented as follows:

The Code is as follows: Copy code

<? Php

$ SearchValue = (int) $ _ GET ['key'];

Function search (array $ array, $ value)
{
$ Max = count ($ array)-1;
$ Min = 0;
$ IsAscSort = $ array [$ min] <$ array [$ max];

While (TRUE ){
$ Sum = $ min + $ max;
$ MidKey = (int) ($ sum % 2 = 1? Ceil ($ sum/2): $ sum/2 );

If ($ max <$ min ){
Return-1;
} Else if ($ value = $ array [$ midKey]) {
Return 1;
} Else if ($ value> $ array [$ midKey]) {
$ IsAscSort? $ Min = $ midKey + 1: $ max = $ midKey-1;
} Else if ($ value <$ array [$ midKey]) {
$ IsAscSort? $ Max = $ midKey-1: $ min = $ midKey + 1;
}
}
}

$ Array = array (
'4', '5', '7', '8', '9', '10', '11', '12'
);
// Forward
Echo search ($ array, $ searchValue );

// Reverse Order
Rsort ($ array );
Echo search ($ array, $ searchValue );

Example 2

PHP searches for small I elements in the array.

The Code is as follows: Copy code

<? Php
# Randomly select a number smaller than I, and implement it in a random fast rank.

# Swap Element
Function swap (& $ arr, $ I, $ j ){
$ Temp = $ arr [$ I];
$ Arr [$ I] = $ arr [$ j];
$ Arr [$ j] = $ temp;
}
 
# Random division
Function randomized_partition (& $ arr, $ begin, $ end ){
$ Rand_tables = rand ($ begin, $ end );
Swap ($ arr, $ begin, $ rand_inx );
Return partition ($ arr, $ begin, $ end );
}
 
# Division
Function partition (& $ arr, $ begin, $ end ){
# Use the first element as the central element
$ Begin = $ begin;
$ Low = $ begin;
$ High = $ end;
 
While ($ low <$ high ){
While ($ low <$ high & $ arr [$ low] <= $ arr [$ slow]) {
$ Low ++;
}
 
While ($ low <$ high & $ arr [$ high] >=$ arr [$ hour]) {
$ High --;
}
 
Swap ($ arr, $ low, $ high );
}
 
# Central element exchange
If ($ arr [$ Scheme] <$ arr [$ low]) {
$ Low --;
}
Swap ($ arr, $ scheme, $ low );
Return $ low;
}
 
# Fast sorting, not used here
Function quick_sort (& $ arr, $ begin, $ end ){
$ Q = randomized_partition ($ arr, $ begin, $ end );
If ($ q> $ begin ){
Quick_sort ($ arr, $ begin, $ q-1 );
}
If ($ q <$ end ){
Quick_sort ($ arr, $ q + 1, $ end );
}
}
 
# Select the number smaller than I
Function randomized_select (& $ arr, $ begin, $ end, $ I ){
If ($ begin ==$ end ){
Return $ arr [$ begin];
}
 
$ Q = randomized_partition ($ arr, $ begin, $ end );
$ K = $ q-$ begin + 1; # k indicates the number of elements less than or equal to q.
 
If ($ k = $ I) {# if k = I, q is the element coordinate smaller than I.
Return $ arr [$ q];
} Else if ($ I <$ k) {# if I <k, the element smaller than I is on the left of q
Return randomized_select ($ arr, $ begin, $ q-1, $ I );
} Else {# The element smaller than I is located on the right of q. In this case, find the element smaller than I-k on the right.
Return randomized_select ($ arr, $ q + 1, $ end, $ I-$ k );
}
}
 
$ Arr = array (1, 5, 3, 7, 0, 0, 8, 4, 2, 9, 11 );
$ T = randomized_select ($ arr, 0, count ($ arr)-1, 8 );
Print_r ("The 8th minimum element: {$ t }");
Echo "<br> ";
Quick_sort ($ arr, 0, count ($ arr)-1 );
Print_r ($ arr );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.