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.
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ông — Searches 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
=
0]: int | chuỗi | Sai[mixed foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false] {
//results here
}
}
$needle
, array $haystack
, bool $strict
=
0]: int|string|falseforeach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false] {
//results here
}
}
Thông số
foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false] {
//results here
}
}
1Giá trị tìm kiếm.
Ghi chú::
Nếu
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 } }
foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false] {
//results here
}
}
3Mảng.
foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false] {
//results here
}
}
4Nế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']];
7Xem 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 }];
5cue 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 }];
9Stefano@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
0function 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 }];
5Azaozz, 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
3function arraySearch[ $array, $search ] {
foreach [$array as $a ] {
if[strstr[ $a, $search]]{
echo $a;
}
}
return false;
}
arraySearch[array["php","mysql","search"],"my"]; // will return mysql
4function arraySearch[ $array, $search ] {
foreach [$array as $a ] {
if[strstr[ $a, $search]]{
echo $a;
}
}
return false;
}
arraySearch[array["php","mysql","search"],"my"]; // will return mysql
5function arraySearch[ $array, $search ] {
foreach [$array as $a ] {
if[strstr[ $a, $search]]{
echo $a;
}
}
return false;
}
arraySearch[array["php","mysql","search"],"my"]; // will return mysql
614 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
7function 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 }];
515 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];
}
}
0function check[$yourString]
{
foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false]
return strpos[$value, $yourString];
}
}
1function check[$yourString]
{
foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false]
return strpos[$value, $yourString];
}
}
2function 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 }];
515 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];
}
}
5function check[$yourString]
{
foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false]
return strpos[$value, $yourString];
}
}
6function check[$yourString]
{
foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false]
return strpos[$value, $yourString];
}
}
7function check[$yourString]
{
foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false]
return strpos[$value, $yourString];
}
}
8function check[$yourString]
{
foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false]
return strpos[$value, $yourString];
}
}
9array_filter
0
array_filter
1
array_filter
2
array_filter
3
array_filter
4
array_filter
5
array_filter
6
$filtered_array = array_filter[$original_array, function[$a]{ return stristr[$a,"jas"]!==false }];
5Array_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_filter
8
array_filter
9
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$needle
1
$needle
2
Array_Search [hỗn hợp $needle
, mảng $haystack
, bool $strict
=
0]: int | chuỗi | Sai ¶foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false] {
//results here
}
}
Thông số
$needle
3
$needle
4
$needle
5
$needle
6
$needle
7
1 ¶foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false] {
//results here
}
}
13 năm trước
$needle
8
Giá trị tìm kiếm. ¶
13 năm trước
$needle
9
$haystack
0
$haystack
1
$haystack
2
$haystack
3
$haystack
4
$haystack
5Helenadeus tại Gmail Dot Com ¶ ¶
13 năm trước
$haystack
6
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$haystack
8
$haystack
9Stooshie tại Gmail Dot Com ¶ ¶
11 năm trước
$strict
0
Azaozz, Gmail ¶ ¶
13 năm trước
$strict
1
$strict
2
$strict
3
$strict
4
$strict
5Andreas Dot Damm tại Maxmachine Dot de ¶ ¶
14 năm trước
$strict
6
Kermes [at] Thesevens [dot] net ¶ ¶
15 năm trước
$strict
7
$strict
8
$strict
9
foreach[$yourArr as $key => $value] {
if [strpos[$value, $yourString] !== false] {
//results here
}
}
00