Tôi thích thực hiện tìm kiếm trên một mảng và trả về tất cả các giá trị khi tìm thấy một trận đấu. Chìa khóa [name]
trong mảng là những gì tôi đang thực hiện tìm kiếm.
Nếu tôi đã tìm kiếm John Doe, nó sẽ trở lại.John Doe it would return.
Sẽ dễ dàng hơn để đổi tên các mảng dựa trên những gì tôi đang tìm kiếm. Thay vì mảng ở trên, tôi cũng có thể tạo ra những điều sau đây.
Tôi muốn đưa ra một thay đổi tùy chọn cho câu trả lời của Scibuff (rất tuyệt vời). Nếu bạn không tìm kiếm một trận đấu chính xác, nhưng một chuỗi bên trong mảng ...
Gọi cái này với ...
Array_Search () là một hàm sẵn có trong PHP được sử dụng để tìm kiếm một giá trị cụ thể trong một mảng và nếu tìm thấy giá trị thì nó sẽ trả về khóa tương ứng của nó. Nếu có nhiều hơn một giá trị thì khóa của giá trị khớp đầu tiên sẽ được trả về.
Sự khác biệt giữa in_array và Array_Search là gì? — Searches the array for a given value and returns the first corresponding key if successful
Sự khác biệt chính giữa cả hai hàm là Array_Search () thường trả về khóa hoặc chỉ mục trong khi in_array () trả về đúng hoặc sai theo kết quả kết quả tìm kiếm trong tìm kiếm.
Làm thế nào để bạn tìm thấy giá trị cụ thể của một mảng?(mixed $needle
, array Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
0, bool Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
1 = Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
2): int|string|false
get () là một phương thức sẵn có trong Java và được sử dụng để trả về phần tử tại một chỉ mục nhất định từ mảng được chỉ định.
(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
Sự mô tả:
Array_Search (hỗn hợp $needle
, mảng
Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
0, bool Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
1 = Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
2): int | chuỗi | Sai
Thông số
Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
3 Giá trị tìm kiếm.Ghi chú:Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
8 then the array_search() function will search for identical elements in the
Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
5. This means it will also perform a
strict type comparison of the Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
3 in the Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
5, and objects must be the same instance. Trả về giá trị
Trả về khóa cho
Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
3 nếu nó được tìm thấy trong mảng, Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
2 khác.Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
2 otherwise. Nếu
Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
3 được tìm thấy trong Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
5 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ố Array (
[Jane Doe] => Array
(
[id] => 20120100
[link] => www.janedoe.com
[name] => Jane Doe
)
[John Doe] => Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
)
6 tùy chọn thay thế.array_keys() with the optional Array (
[Jane Doe] => Array
(
[id] => 20120100
[link] => www.janedoe.com
[name] => Jane Doe
)
[John Doe] => Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
)
6 parameter instead. Cảnh báo
Hàm này có thể trả về Boolean
Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
2, nhưng cũng có thể trả về giá trị phi Boolean đánh giá thành Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
2. 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.Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
2, but may also return a non-Boolean value which evaluates to Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
2. 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
Array (
[Jane Doe] => Array
(
[id] => 20120100
[link] => www.janedoe.com
[name] => Jane Doe
)
[John Doe] => Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
)
9Xem 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
$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
0$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
1$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
2$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
3$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
4$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
5$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
6$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
7 cue tại openxbox dot com ¶ ¶
19 năm trước
$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
8$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
9function array_search_x( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if (strpos($item['name'], $name) !== false) { // changed this line
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
0function array_search_x( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if (strpos($item['name'], $name) !== false) { // changed this line
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
1 Stefano@takys chấm nó ¶ ¶
11 năm trước
function array_search_x( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if (strpos($item['name'], $name) !== false) { // changed this line
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
2function array_search_x( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if (strpos($item['name'], $name) !== false) { // changed this line
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
3$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
7
opencart dot ocfilter tại gmail dot com ¶ ¶
1 năm trước
function array_search_x( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if (strpos($item['name'], $name) !== false) { // changed this line
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
5function array_search_x( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if (strpos($item['name'], $name) !== false) { // changed this line
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
6function array_search_x( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if (strpos($item['name'], $name) !== false) { // changed this line
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
7function array_search_x( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if (strpos($item['name'], $name) !== false) { // changed this line
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
8 Nordsebaer tại gmx dot de ¶ ¶
2 năm trước
function array_search_x( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if (strpos($item['name'], $name) !== false) { // changed this line
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
9$pc_ct = array_search_x($your_array_name, 'your_string_here');
0$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
7 Yasien Dot Dwieb tại Gmail Dot Com ¶ ¶
2 năm trước
$pc_ct = array_search_x($your_array_name, 'your_string_here');
2$pc_ct = array_search_x($your_array_name, 'your_string_here');
3$pc_ct = array_search_x($your_array_name, 'your_string_here');
4$pc_ct = array_search_x($your_array_name, 'your_string_here');
5$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
7 Yasien Dot Dwieb tại Gmail Dot Com ¶ ¶
Thinbegin tại Gmail Dot Com ¶
$pc_ct = array_search_x($your_array_name, 'your_string_here');
7$pc_ct = array_search_x($your_array_name, 'your_string_here');
8$pc_ct = array_search_x($your_array_name, 'your_string_here');
9function search_array( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if ( $item['name'] == $name ){ // or other string comparison
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
0function search_array( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if ( $item['name'] == $name ){ // or other string comparison
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
1function search_array( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if ( $item['name'] == $name ){ // or other string comparison
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
2function search_array( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if ( $item['name'] == $name ){ // or other string comparison
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
3function search_array( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if ( $item['name'] == $name ){ // or other string comparison
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
4function search_array( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if ( $item['name'] == $name ){ // or other string comparison
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
5function search_array( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if ( $item['name'] == $name ){ // or other string comparison
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
6function search_array( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if ( $item['name'] == $name ){ // or other string comparison
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
7function search_array( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if ( $item['name'] == $name ){ // or other string comparison
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
8$filteredArray =
array_filter($array, function($element) use($searchFor){
return isset($element['name']) && $element['name'] == $searchFor;
});
7 4 năm trước ¶
maciej tại speccode dot com
$array = Array (
[0] => Array(
[id] => 20120100,
[link] => www.janedoe.com,
[name] => 'Jane Doe'
),
[1] => Array(
[id] => 20120101,
[link] => www.johndoe.com,
[name] => 'John Doe'
)
);
$i = 0;
$new_array = [];
foreach($array as $key => $value){
if($value['name'] === 'John Doe'){
$key = $value['name'].$i;
$new_array[] = [$key => $value];
$i ++;
}
}
return $new_array;
0$array = Array (
[0] => Array(
[id] => 20120100,
[link] => www.janedoe.com,
[name] => 'Jane Doe'
),
[1] => Array(
[id] => 20120101,
[link] => www.johndoe.com,
[name] => 'John Doe'
)
);
$i = 0;
$new_array = [];
foreach($array as $key => $value){
if($value['name'] === 'John Doe'){
$key = $value['name'].$i;
$new_array[] = [$key => $value];
$i ++;
}
}
return $new_array;
1function array_search_x( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if (strpos($item['name'], $name) !== false) { // changed this line
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
6$array = Array (
[0] => Array(
[id] => 20120100,
[link] => www.janedoe.com,
[name] => 'Jane Doe'
),
[1] => Array(
[id] => 20120101,
[link] => www.johndoe.com,
[name] => 'John Doe'
)
);
$i = 0;
$new_array = [];
foreach($array as $key => $value){
if($value['name'] === 'John Doe'){
$key = $value['name'].$i;
$new_array[] = [$key => $value];
$i ++;
}
}
return $new_array;
3$array = Array (
[0] => Array(
[id] => 20120100,
[link] => www.janedoe.com,
[name] => 'Jane Doe'
),
[1] => Array(
[id] => 20120101,
[link] => www.johndoe.com,
[name] => 'John Doe'
)
);
$i = 0;
$new_array = [];
foreach($array as $key => $value){
if($value['name'] === 'John Doe'){
$key = $value['name'].$i;
$new_array[] = [$key => $value];
$i ++;
}
}
return $new_array;
4
7 năm trước ¶
Richgc ¶
$array = Array (
[0] => Array(
[id] => 20120100,
[link] => www.janedoe.com,
[name] => 'Jane Doe'
),
[1] => Array(
[id] => 20120101,
[link] => www.johndoe.com,
[name] => 'John Doe'
)
);
$i = 0;
$new_array = [];
foreach($array as $key => $value){
if($value['name'] === 'John Doe'){
$key = $value['name'].$i;
$new_array[] = [$key => $value];
$i ++;
}
}
return $new_array;
5$array = Array (
[0] => Array(
[id] => 20120100,
[link] => www.janedoe.com,
[name] => 'Jane Doe'
),
[1] => Array(
[id] => 20120101,
[link] => www.johndoe.com,
[name] => 'John Doe'
)
);
$i = 0;
$new_array = [];
foreach($array as $key => $value){
if($value['name'] === 'John Doe'){
$key = $value['name'].$i;
$new_array[] = [$key => $value];
$i ++;
}
}
return $new_array;
6$array = Array (
[0] => Array(
[id] => 20120100,
[link] => www.janedoe.com,
[name] => 'Jane Doe'
),
[1] => Array(
[id] => 20120101,
[link] => www.johndoe.com,
[name] => 'John Doe'
)
);
$i = 0;
$new_array = [];
foreach($array as $key => $value){
if($value['name'] === 'John Doe'){
$key = $value['name'].$i;
$new_array[] = [$key => $value];
$i ++;
}
}
return $new_array;
7$array = Array (
[0] => Array(
[id] => 20120100,
[link] => www.janedoe.com,
[name] => 'Jane Doe'
),
[1] => Array(
[id] => 20120101,
[link] => www.johndoe.com,
[name] => 'John Doe'
)
);
$i = 0;
$new_array = [];
foreach($array as $key => $value){
if($value['name'] === 'John Doe'){
$key = $value['name'].$i;
$new_array[] = [$key => $value];
$i ++;
}
}
return $new_array;
8$array = Array (
[0] => Array(
[id] => 20120100,
[link] => www.janedoe.com,
[name] => 'Jane Doe'
),
[1] => Array(
[id] => 20120101,
[link] => www.johndoe.com,
[name] => 'John Doe'
)
);
$i = 0;
$new_array = [];
foreach($array as $key => $value){
if($value['name'] === 'John Doe'){
$key = $value['name'].$i;
$new_array[] = [$key => $value];
$i ++;
}
}
return $new_array;
9 16 năm trước ¶
n-regen ¶
[name]
0
13 năm trước ¶
n-regen ¶
[name]
1
[name]
2
[name]
3
[name]
4
[name]
5
[name]
6
[name]
7
13 năm trước ¶
n-regen ¶
[name]
8
function array_search_x( $array, $name ){
foreach( $array as $item ){
if ( is_array( $item ) && isset( $item['name'] )){
if (strpos($item['name'], $name) !== false) { // changed this line
return $item;
}
}
}
return FALSE; // or whatever else you'd like
}
6$needle
0
$needle
1 13 năm trước ¶
11 năm trước
$needle
2
opencart dot ocfilter tại gmail dot com ¶ ¶
n-regen ¶
$needle
3
$needle
4
$needle
5
$needle
6
$needle
7 13 năm trước ¶
CodeSlinger tại compsalot dot com
$needle
8
Helenadeus tại Gmail Dot Com ¶ ¶
Stooshie tại Gmail Dot Com ¶
$needle
9
Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
00Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
01Array
(
[id] => 20120101
[link] => www.johndoe.com
[name] => John Doe
)
02Để kiểm tra xem có tồn tại nhiều giá trị trong một mảng không: sử dụng phương thức mỗi () để lặp qua mảng giá trị hay không. Nếu tất cả các giá trị tồn tại trong mảng, mọi phương thức sẽ trả về true.Use the every() method to iterate over the array of values. On each iteration, use the indexOf method to check if the value is contained in the other array. If all values exist in the array, the every method will return true .
Array_Search () là một hàm sẵn có trong PHP được sử dụng để tìm kiếm một giá trị cụ thể trong một mảng và nếu tìm thấy giá trị thì nó sẽ trả về khóa tương ứng của nó.Nếu có nhiều hơn một giá trị thì khóa của giá trị khớp đầu tiên sẽ được trả về.an inbuilt function in PHP that is used to search for a particular value in an array, and if the value is found then it returns its corresponding key. If there are more than one values then the key of the first matching value will be returned.
Sự khác biệt chính giữa cả hai hàm là Array_Search () thường trả về khóa hoặc chỉ mục trong khi in_array () trả về đúng hoặc sai theo kết quả kết quả tìm kiếm trong tìm kiếm.array_search() usually returns either key or index whereas in_array() returns TRUE or FALSE according to match found in search.