Hướng dẫn get index of array php - lấy chỉ mục của mảng php

// hoặc xem xét cấu trúc mảng của bạn:

Show
$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

Sự mô tả:

Array_Search (hỗn hợp $needle, mảng $haystack, bool $strict = false): int | chuỗi | Sai

Thông số

function findIndexOfKey($key_to_index,$array){
  return array_search($key_to_index,array_keys($array));
}
0

Giá trị tìm kiếm.

Ghi chú:

function findIndexOfKey($key_to_index,$array){
  return array_search($key_to_index,array_keys($array));
}
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));
}
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));
}
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.

function findIndexOfKey($key_to_index,$array){
  return array_search($key_to_index,array_keys($array));
}
3

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

function findIndexOfKey($key_to_index,$array){
  return array_search($key_to_index,array_keys($array));
}
0 nếu nó được tìm thấy trong mảng, 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ế.

  • 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ành false. 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";
7

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";
8

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";
9

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";
0

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";
1

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";
2

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";
3

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";
4

cue 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";
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";
6

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";
7

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";
8

Stefano@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";
9

print "\r\n//-- Method 3 --//\r\n";
foreach(array_keys($array) as $key => $value){
  print '#index of: '.$value.' = '.$key."\r\n";
}
0

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";
4

opencart 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";
}
2

print "\r\n//-- Method 3 --//\r\n";
foreach(array_keys($array) as $key => $value){
  print '#index of: '.$value.' = '.$key."\r\n";
}
3

print "\r\n//-- Method 3 --//\r\n";
foreach(array_keys($array) as $key => $value){
  print '#index of: '.$value.' = '.$key."\r\n";
}
4

print "\r\n//-- Method 3 --//\r\n";
foreach(array_keys($array) as $key => $value){
  print '#index of: '.$value.' = '.$key."\r\n";
}
5

Nordsebaer 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";
}
6

print "\r\n//-- Method 3 --//\r\n";
foreach(array_keys($array) as $key => $value){
  print '#index of: '.$value.' = '.$key."\r\n";
}
7

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";
4

Yasien 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
2

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";
4

Yasien 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

$needle0

$needle1

$needle2

$needle3

$needle4

$needle5

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";
4

maciej tại speccode dot com

7 năm trước

$needle7

$needle8

print "\r\n//-- Method 3 --//\r\n";
foreach(array_keys($array) as $key => $value){
  print '#index of: '.$value.' = '.$key."\r\n";
}
3

$haystack0

$haystack1

Richgc ¶

16 năm trước

$haystack2

$haystack3

$haystack4

$haystack5

$haystack6

n-regen ¶

13 năm trước

$haystack7

CodeSlinger tại compsalot dot com

13 năm trước

$haystack8

$haystack9

$strict0

$strict1

$strict2

$strict3

$strict4

CodeSlinger tại compsalot dot com

13 năm trước

$strict5

print "\r\n//-- Method 3 --//\r\n";
foreach(array_keys($array) as $key => $value){
  print '#index of: '.$value.' = '.$key."\r\n";
}
3

$strict7

$strict8

CodeSlinger tại compsalot dot com

Helenadeus tại Gmail Dot Com ¶

$strict9

Stooshie tại Gmail Dot Com ¶

13 năm trước

false0

false1

false2

false3

false4

CodeSlinger tại compsalot dot com

Helenadeus tại Gmail Dot Com ¶

false5

Stooshie tại Gmail Dot Com ¶

11 năm trước

false6

false7

false8

false9

Làm thế nào để bạn có được chỉ mục của một phần tử trong một mảng trong PHP?

Chúng ta có thể nhận chỉ mục mảng bằng cách sử dụng hàm mảng_search ().Hàm này được sử dụng để tìm kiếm phần tử đã cho.using the array_search() function. This function is used to search for the given element.

Array_Keys () được sử dụng để làm gì?

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.to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.

Key $ trong PHP là gì?

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.returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns null .

Chỉ số của một mảng là gì?

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ố.indicates the position of the element within the array (starting from 1) and is either a number or a field containing a number.