Hướng dẫn array search with condition php - tìm kiếm mảng với điều kiện php

Tôi đang có mảng sau và tôi muốn sử dụng Search và Sắp xếp. Nghiên cứu và sắp xếp giống như sắp xếp mà chúng tôi thực hiện với điều kiện MySQL "Like" nhưng trong mảng không có trong cơ sở dữ liệu.

Array
(
    [4] => Varun Kumar
    [14] => Jason Ince
)

Giống như khi gõ bản ghi 'Jas' với Jason Ince phải ra khỏi nó với các khóa và giá trị và phần còn lại của bản ghi tương ứng.

Hướng dẫn array search with condition php - tìm kiếm mảng với điều kiện php

Poncha

7.5982 Huy hiệu vàng33 Huy hiệu bạc38 Huy hiệu đồng2 gold badges33 silver badges38 bronze badges

hỏi ngày 16 tháng 7 năm 2012 lúc 5:36Jul 16, 2012 at 5:36

Intekhab Khanintekhab KhanIntekhab Khan

1.7354 huy hiệu vàng18 Huy hiệu bạc28 Huy hiệu đồng4 gold badges18 silver badges28 bronze badges

1

Ý bạn là một cái gì đó như:

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}

Đã trả lời ngày 16 tháng 7 năm 2012 lúc 5:43Jul 16, 2012 at 5:43

Sudhir Bastakotisudhir BastakotiSudhir Bastakoti

98K15 Huy hiệu vàng156 Huy hiệu bạc161 Huy hiệu Đồng15 gold badges156 silver badges161 bronze badges

1

Bạn có thể sử dụng array_filter:

$filtered_array = array_filter($original_array, create_function($a, 'return stristr($a,"jas")!==false'));

Hoặc, nếu bạn đang sử dụng Php 5.3+, cú pháp là:

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });

Đã trả lời ngày 16 tháng 7 năm 2012 lúc 5:43Jul 16, 2012 at 5:43

function arraySearch( $array, $search ) { 
    foreach ($array as $a ) { 
        if(strstr( $a, $search)){ 
            echo $a;
        } 
    } 
return false; 
}
arraySearch(array("php","mysql","search"),"my"); // will return mysql

Sudhir Bastakotisudhir BastakotiJul 16, 2012 at 5:45

98K15 Huy hiệu vàng156 Huy hiệu bạc161 Huy hiệu ĐồngSanjay

Bạn có thể sử dụng array_filter:1 gold badge13 silver badges29 bronze badges

Hoặc, nếu bạn đang sử dụng Php 5.3+, cú pháp là:

function check($yourString)
{
    foreach($yourArr as $key => $value) {
        if (strpos($value, $yourString) !== false)
            return strpos($value, $yourString);
    }
}

Đã trả lời ngày 16 tháng 7 năm 2012 lúc 5:45

SanjaysanjayJul 16, 2012 at 5:46

(Php 4> = 4.0.5, Php 5, Php 7, Php 8)

Array_Search - Tìm kiếm mảng cho một giá trị đã cho và trả về khóa tương ứng đầu tiên nếu thành côngSearches the array for a given value and returns the first corresponding key if successful

Sự mô tả

Array_Search (hỗn hợp $needle, mảng $haystack, bool $strict =

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
0): int | chuỗi | Sai(mixed $needle, array $haystack, bool $strict =
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
0
): int|string|false

Thông số

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
1

Giá trị tìm kiếm.

Ghi chú::

Nếu

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
1 là một chuỗi, so sánh được thực hiện theo cách nhạy cảm trường hợp.

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
3

Mảng.

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
4

Nếu tham số thứ ba

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
4 được đặt thành
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
6 thì hàm mảng_search () sẽ tìm kiếm các phần tử giống hệt nhau trong
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
3. Điều này có nghĩa là nó cũng sẽ thực hiện so sánh loại nghiêm ngặt của
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
1 trong
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
3 và các đối tượng phải là cùng một trường hợp.
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
6
then the array_search() function will search for identical elements in the
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
3. This means it will also perform a strict type comparison of the
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
1 in the
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
3, and objects must be the same instance.

Trả về giá trị

Trả về khóa cho

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
1 nếu nó được tìm thấy trong mảng,
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
0 khác.
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
0
otherwise.

Nếu

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
1 được tìm thấy trong
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
3 nhiều lần, khóa khớp đầu tiên được trả về. Để trả về các khóa cho tất cả các giá trị khớp, hãy sử dụng Array_Keys () với tham số
$filtered_array = array_filter($original_array, create_function($a, 'return stristr($a,"jas")!==false'));
4 tùy chọn thay thế.array_keys() with the optional
$filtered_array = array_filter($original_array, create_function($a, 'return stristr($a,"jas")!==false'));
4 parameter instead.

Cảnh báo

Hàm này có thể trả về Boolean ____10, nhưng cũng có thể trả về giá trị phi Boolean đánh giá thành

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
0. Vui lòng đọc phần về Booleans để biết thêm thông tin. Sử dụng toán tử === để kiểm tra giá trị trả về của hàm này.
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
0
, but may also return a non-Boolean value which evaluates to
foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
0
. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Ví dụ

Ví dụ #1 Array_Search () ví dụarray_search() example

$filtered_array = array_filter($original_array, create_function($a, 'return stristr($a,"jas")!==false'));
7

Xem thêm

  • Array_Keys () - Trả về tất cả các phím hoặc một tập hợp con của các phím của một mảng
  • Array_Values ​​() - Trả về tất cả các giá trị của một mảng
  • Array_Key_Exists () - Kiểm tra xem khóa hoặc chỉ mục đã cho có tồn tại trong mảng
  • in_array () - kiểm tra xem giá trị có tồn tại trong một mảng không

Turabgarip tại Gmail Dot Com ¶

5 năm trước

$filtered_array = array_filter($original_array, create_function($a, 'return stristr($a,"jas")!==false'));
8

$filtered_array = array_filter($original_array, create_function($a, 'return stristr($a,"jas")!==false'));
9

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
0

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
1

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
2

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
3

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
4

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
5

cue tại openxbox dot com ¶

19 năm trước

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
6

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
7

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
8

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
9

Stefano@takys chấm nó ¶

11 năm trước

function arraySearch( $array, $search ) { 
    foreach ($array as $a ) { 
        if(strstr( $a, $search)){ 
            echo $a;
        } 
    } 
return false; 
}
arraySearch(array("php","mysql","search"),"my"); // will return mysql
0

function arraySearch( $array, $search ) { 
    foreach ($array as $a ) { 
        if(strstr( $a, $search)){ 
            echo $a;
        } 
    } 
return false; 
}
arraySearch(array("php","mysql","search"),"my"); // will return mysql
1

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
5

Azaozz, Gmail ¶

Andreas Dot Damm tại Maxmachine Dot de ¶

function arraySearch( $array, $search ) { 
    foreach ($array as $a ) { 
        if(strstr( $a, $search)){ 
            echo $a;
        } 
    } 
return false; 
}
arraySearch(array("php","mysql","search"),"my"); // will return mysql
3

function arraySearch( $array, $search ) { 
    foreach ($array as $a ) { 
        if(strstr( $a, $search)){ 
            echo $a;
        } 
    } 
return false; 
}
arraySearch(array("php","mysql","search"),"my"); // will return mysql
4

function arraySearch( $array, $search ) { 
    foreach ($array as $a ) { 
        if(strstr( $a, $search)){ 
            echo $a;
        } 
    } 
return false; 
}
arraySearch(array("php","mysql","search"),"my"); // will return mysql
5

function arraySearch( $array, $search ) { 
    foreach ($array as $a ) { 
        if(strstr( $a, $search)){ 
            echo $a;
        } 
    } 
return false; 
}
arraySearch(array("php","mysql","search"),"my"); // will return mysql
6

14 năm trước

Kermes [at] Thesevens [dot] net ¶

function arraySearch( $array, $search ) { 
    foreach ($array as $a ) { 
        if(strstr( $a, $search)){ 
            echo $a;
        } 
    } 
return false; 
}
arraySearch(array("php","mysql","search"),"my"); // will return mysql
7

function arraySearch( $array, $search ) { 
    foreach ($array as $a ) { 
        if(strstr( $a, $search)){ 
            echo $a;
        } 
    } 
return false; 
}
arraySearch(array("php","mysql","search"),"my"); // will return mysql
8

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
5

15 năm trước

Kermes [at] Thesevens [dot] net ¶

function check($yourString)
{
    foreach($yourArr as $key => $value) {
        if (strpos($value, $yourString) !== false)
            return strpos($value, $yourString);
    }
}
0

function check($yourString)
{
    foreach($yourArr as $key => $value) {
        if (strpos($value, $yourString) !== false)
            return strpos($value, $yourString);
    }
}
1

function check($yourString)
{
    foreach($yourArr as $key => $value) {
        if (strpos($value, $yourString) !== false)
            return strpos($value, $yourString);
    }
}
2

function check($yourString)
{
    foreach($yourArr as $key => $value) {
        if (strpos($value, $yourString) !== false)
            return strpos($value, $yourString);
    }
}
3

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
5

15 năm trước

(Php 4> = 4.0.5, Php 5, Php 7, Php 8)

function check($yourString)
{
    foreach($yourArr as $key => $value) {
        if (strpos($value, $yourString) !== false)
            return strpos($value, $yourString);
    }
}
5

function check($yourString)
{
    foreach($yourArr as $key => $value) {
        if (strpos($value, $yourString) !== false)
            return strpos($value, $yourString);
    }
}
6

function check($yourString)
{
    foreach($yourArr as $key => $value) {
        if (strpos($value, $yourString) !== false)
            return strpos($value, $yourString);
    }
}
7

function check($yourString)
{
    foreach($yourArr as $key => $value) {
        if (strpos($value, $yourString) !== false)
            return strpos($value, $yourString);
    }
}
8

function check($yourString)
{
    foreach($yourArr as $key => $value) {
        if (strpos($value, $yourString) !== false)
            return strpos($value, $yourString);
    }
}
9

array_filter0

array_filter1

array_filter2

array_filter3

array_filter4

array_filter5

array_filter6

$filtered_array = array_filter($original_array, function($a){ return stristr($a,"jas")!==false });
5

Array_Search - Tìm kiếm mảng cho một giá trị đã cho và trả về khóa tương ứng đầu tiên nếu thành công

Sự mô tả

array_filter8

array_filter9

function arraySearch( $array, $search ) { 
    foreach ($array as $a ) { 
        if(strstr( $a, $search)){ 
            echo $a;
        } 
    } 
return false; 
}
arraySearch(array("php","mysql","search"),"my"); // will return mysql
4

$needle1

$needle2

Array_Search (hỗn hợp $needle, mảng $haystack, bool $strict =

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
0): int | chuỗi | Sai

Thông số

$needle3

$needle4

$needle5

$needle6

$needle7

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
1

13 năm trước

$needle8

Giá trị tìm kiếm.

13 năm trước

$needle9

$haystack0

$haystack1

$haystack2

$haystack3

$haystack4

$haystack5

Helenadeus tại Gmail Dot Com ¶

13 năm trước

$haystack6

function arraySearch( $array, $search ) { 
    foreach ($array as $a ) { 
        if(strstr( $a, $search)){ 
            echo $a;
        } 
    } 
return false; 
}
arraySearch(array("php","mysql","search"),"my"); // will return mysql
4

$haystack8

$haystack9

Stooshie tại Gmail Dot Com ¶

11 năm trước

$strict0

Azaozz, Gmail ¶

13 năm trước

$strict1

$strict2

$strict3

$strict4

$strict5

Andreas Dot Damm tại Maxmachine Dot de ¶

14 năm trước

$strict6

Kermes [at] Thesevens [dot] net ¶

15 năm trước

$strict7

$strict8

$strict9

foreach($yourArr as $key => $value) {
    if (strpos($value, $yourString) !== false) {
       //results here
    }
}
00