// hoặc xem xét cấu trúc mảng của bạn:
$array = array[
'string1' => array['a' => '', 'b' => '', 'c' => ''],
'string2' => array['a' => '', 'b' => '', 'c' => ''],
'string3' => array['a' => '', 'b' => '', 'c' => ''],
];
// Bạn chỉ có thể
function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
// Thực thi
print "\r\n//-- Method 1 --//\r\n";
print '#index of: string1 = '.findIndexofKey['string1',$array]."\r\n";
print '#index of: string2 = '.findIndexofKey['string2',$array]."\r\n";
print '#index of: string3 = '.findIndexofKey['string3',$array]."\r\n";
// cách khác
print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
// đệ quy
print "\r\n//-- Method 3 --//\r\n";
foreach[array_keys[$array] as $key => $value]{
print '#index of: '.$value.' = '.$key."\r\n";
}
// đầu ra
//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
Array_Keys [] là một hàm tích hợp trong PHP và được sử dụng để trả về tất cả các khóa và mảng hoặc tập hợp con của các khóa. Tham số: Hàm lấy ba tham số trong đó một tham số là bắt buộc và hai tham số khác là tùy chọn.
Key $ trong PHP là gì? — Searches the array for a given value and returns the first corresponding key if successful
Giá trị trả về ¶ Chức năng [] hàm chỉ đơn giản là trả về khóa của phần tử mảng hiện đang được chỉ ra bởi con trỏ bên trong. Nó không di chuyển con trỏ theo bất kỳ cách nào. Nếu con trỏ bên trong các điểm vượt ra ngoài phần cuối của danh sách các phần tử hoặc mảng trống, key [] trả về null.
Chỉ số của một mảng là gì?[mixed $needle
, array $haystack
, bool $strict
= false
]: int|string|false
Chỉ số chỉ ra vị trí của phần tử trong mảng [bắt đầu từ 1] và là một số hoặc một trường chứa một số.
[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
Thông sốSự mô tả:
Array_Search [hỗn hợp
$needle
, mảng$haystack
, bool$strict
=false
]: int | chuỗi | Sai
function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
0 Giá trị tìm kiếm.Ghi chú:
5 then the array_search[] function will search for identical elements in the function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
2. This means it will also perform a
strict type comparison of the function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
0 in the function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
2, and objects must be the same instance. Nếu function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
0 là một chuỗi, so sánh được thực hiện theo cách nhạy cảm trường hợp.
function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
2false
otherwise. Mảng.array_keys[] with the optional
print "\r\n//-- Method 1 --//\r\n";
print '#index of: string1 = '.findIndexofKey['string1',$array]."\r\n";
print '#index of: string2 = '.findIndexofKey['string2',$array]."\r\n";
print '#index of: string3 = '.findIndexofKey['string3',$array]."\r\n";
3 parameter instead.
3function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
Nếu tham số thứ ba
function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
3 được đặt thành function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
5 thì hàm mảng_search [] sẽ tìm kiếm các phần tử giống hệt nhau trong function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
2. Đ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 function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
0 trong function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
2 và các đối tượng phải là cùng một ví dụ.false
, but may also return a non-Boolean value which evaluates to false
. Please read the section on Booleans for more
information. Use the === operator for testing the return value of this function.Trả về giá trị
Trả về khóa cho
0 nếu nó được tìm thấy trong mảng, function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
false
khác.array_search[] example
print "\r\n//-- Method 1 --//\r\n";
print '#index of: string1 = '.findIndexofKey['string1',$array]."\r\n";
print '#index of: string2 = '.findIndexofKey['string2',$array]."\r\n";
print '#index of: string3 = '.findIndexofKey['string3',$array]."\r\n";
6 Nếu function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
0 được tìm thấy trong function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
2 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ố print "\r\n//-- Method 1 --//\r\n";
print '#index of: string1 = '.findIndexofKey['string1',$array]."\r\n";
print '#index of: string2 = '.findIndexofKey['string2',$array]."\r\n";
print '#index of: string3 = '.findIndexofKey['string3',$array]."\r\n";
3 tùy chọn thay thế.
function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
function findIndexOfKey[$key_to_index,$array]{
return array_search[$key_to_index,array_keys[$array]];
}
print "\r\n//-- Method 1 --//\r\n";
print '#index of: string1 = '.findIndexofKey['string1',$array]."\r\n";
print '#index of: string2 = '.findIndexofKey['string2',$array]."\r\n";
print '#index of: string3 = '.findIndexofKey['string3',$array]."\r\n";
- Cảnh báo
- Hàm này có thể trả về Boolean
false
, nhưng cũng có thể trả về giá trị phi Boolean đánh giá thànhfalse
. 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_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
print "\r\n//-- Method 1 --//\r\n";
print '#index of: string1 = '.findIndexofKey['string1',$array]."\r\n";
print '#index of: string2 = '.findIndexofKey['string2',$array]."\r\n";
print '#index of: string3 = '.findIndexofKey['string3',$array]."\r\n";
7print "\r\n//-- Method 1 --//\r\n";
print '#index of: string1 = '.findIndexofKey['string1',$array]."\r\n";
print '#index of: string2 = '.findIndexofKey['string2',$array]."\r\n";
print '#index of: string3 = '.findIndexofKey['string3',$array]."\r\n";
8print "\r\n//-- Method 1 --//\r\n";
print '#index of: string1 = '.findIndexofKey['string1',$array]."\r\n";
print '#index of: string2 = '.findIndexofKey['string2',$array]."\r\n";
print '#index of: string3 = '.findIndexofKey['string3',$array]."\r\n";
9print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
0print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
1print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
2print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
3print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
4cue tại openxbox dot com ¶ ¶
19 năm trước
print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
5print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
6print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
7print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
8Stefano@takys chấm nó ¶ ¶
11 năm trước
print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
9print "\r\n//-- Method 3 --//\r\n";
foreach[array_keys[$array] as $key => $value]{
print '#index of: '.$value.' = '.$key."\r\n";
}
0print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
4opencart dot ocfilter tại gmail dot com ¶ ¶
1 năm trước
print "\r\n//-- Method 3 --//\r\n";
foreach[array_keys[$array] as $key => $value]{
print '#index of: '.$value.' = '.$key."\r\n";
}
2print "\r\n//-- Method 3 --//\r\n";
foreach[array_keys[$array] as $key => $value]{
print '#index of: '.$value.' = '.$key."\r\n";
}
3print "\r\n//-- Method 3 --//\r\n";
foreach[array_keys[$array] as $key => $value]{
print '#index of: '.$value.' = '.$key."\r\n";
}
4print "\r\n//-- Method 3 --//\r\n";
foreach[array_keys[$array] as $key => $value]{
print '#index of: '.$value.' = '.$key."\r\n";
}
5Nordsebaer tại gmx dot de ¶ ¶
2 năm trước
print "\r\n//-- Method 3 --//\r\n";
foreach[array_keys[$array] as $key => $value]{
print '#index of: '.$value.' = '.$key."\r\n";
}
6print "\r\n//-- Method 3 --//\r\n";
foreach[array_keys[$array] as $key => $value]{
print '#index of: '.$value.' = '.$key."\r\n";
}
7print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
4Yasien Dot Dwieb tại Gmail Dot Com ¶ ¶
2 năm trước
print "\r\n//-- Method 3 --//\r\n";
foreach[array_keys[$array] as $key => $value]{
print '#index of: '.$value.' = '.$key."\r\n";
}
9//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
0//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
1//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
2print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
4Yasien Dot Dwieb tại Gmail Dot Com ¶ ¶
Thinbegin tại Gmail Dot Com ¶
//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
4//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
5//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
6//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
7//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
8//-- Method 1 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 2 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
//-- Method 3 --//
#index of: string1 = 0
#index of: string2 = 1
#index of: string3 = 2
9$needle
0
$needle
1
$needle
2
$needle
3
$needle
4
$needle
5
print "\r\n//-- Method 2 --//\r\n";
print '#index of: string1 = '.array_search['string1',array_keys[$array]]."\r\n";
print '#index of: string2 = '.array_search['string2',array_keys[$array]]."\r\n";
print '#index of: string3 = '.array_search['string3',array_keys[$array]]."\r\n";
4maciej tại speccode dot com ¶
7 năm trước
$needle
7
$needle
8
print "\r\n//-- Method 3 --//\r\n";
foreach[array_keys[$array] as $key => $value]{
print '#index of: '.$value.' = '.$key."\r\n";
}
3$haystack
0
$haystack
1Richgc ¶ ¶
16 năm trước
$haystack
2
$haystack
3
$haystack
4
$haystack
5
$haystack
6n-regen ¶ ¶
13 năm trước
$haystack
7
CodeSlinger tại compsalot dot com ¶
13 năm trước
$haystack
8
$haystack
9
$strict
0
$strict
1
$strict
2
$strict
3
$strict
4CodeSlinger tại compsalot dot com ¶
13 năm trước
$strict
5
print "\r\n//-- Method 3 --//\r\n";
foreach[array_keys[$array] as $key => $value]{
print '#index of: '.$value.' = '.$key."\r\n";
}
3$strict
7
$strict
8CodeSlinger tại compsalot dot com ¶
Helenadeus tại Gmail Dot Com ¶
$strict
9
Stooshie tại Gmail Dot Com ¶ ¶
13 năm trước
false
0
false
1
false
2
false
3
false
4CodeSlinger tại compsalot dot com ¶
Helenadeus tại Gmail Dot Com ¶
false
5
Stooshie tại Gmail Dot Com ¶ ¶
11 năm trước
false
6
false
7
false
8
false
9