You should use the key() function.

key($array)

should return the current key.

If you need the position of the current key:

array_search($key, array_keys($array));
Answer from Zahymaka on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-find-the-index-of-an-element-in-an-array-using-php
How to find the index of an element in an array using PHP ? - GeeksforGeeks
August 26, 2024 - Using a loop to find the index of an element involves iterating through the array with a foreach loop. Check each value, and if it matches the target element, store the current key as the index and exit the loop.
🌐
W3Schools
w3schools.com › php › php_arrays_indexed.asp
PHP Indexed Arrays
To loop through and print all the values of an indexed array, use a foreach loop, like this: Display all array items: $cars = array("Volvo", "BMW", "Toyota"); foreach ($cars as $x) { echo "$x <br>"; } Try it Yourself » · For a complete reference of all array functions, go to our complete PHP Array Reference. ❮ Previous Next ❯ · ★ +1 · Sign in to track progress · REMOVE ADS · PLUS · SPACES · GET CERTIFIED ·
🌐
PHP
php.net › manual › en › function.array-search.php
PHP: array_search - Manual
It's really important to check the return value is not false! I used array_search() to determine the index of an value to unset this value and then realized that $arr[false] === $arr[0] !
🌐
IncludeHelp
includehelp.com › php › how-to-find-the-index-of-an-element-in-an-array-using-php.aspx
How to find the index of an element in an array using PHP?
Now, let's consider an associative array that consists of countries and their capitals. We have to get the index of a capital, namely "Tokyo" from the given array. Here's how you can use the array_search() function for this purpose: <?php // These lines of code create an associative array // ...
🌐
W3Schools
w3schools.com › php › func_array_search.asp
PHP array_search() Function
PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat Parser PHP DOM Parser · AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll · PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Server PHP Syllabus PHP Study Plan PHP Certificate ... array() array_change_key_case() array_chunk() array_column() array_combine() array_count_values() array_diff() array_diff_assoc() array_diff_key() array_diff_uassoc() array_diff_ukey() array_fill() array_fill_keys() array_filter() array_flip() array_intersect() array_intersect_assoc() array_intersect_key() array_intersect
🌐
W3Resource
w3resource.com › php-exercises › php-array-exercise-31.php
PHP Array Exercise: Get the index of the highest value in an associative array - w3resource
<?php // Define an associative ... keys arsort($x); // Get the key of the maximum (first) value in the sorted array $key_of_max = key($x); // Display the index (key) of the highest value in the original array echo "Index of ...
🌐
TutorialKart
tutorialkart.com › php › php-find-index-of-value-in-array
Find Index of Value in Array in PHP
May 8, 2023 - To find the index of specific value in given array in PHP, we can use array_search() function. array_search() function takes the value and array as arguments, and returns the key of the first occurrence of the value.
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 2076461 › return-array-index-in-php-without-any-functions
Return array index in PHP WITHOUT ANY FUNCTIONS | Sololearn: Learn to code for FREE!
Then for each row we get number of columns. Having both number of rows and columns we can do the loops just fine. But mind you this is not something you wanna use when there are loads of data, just not sure about efficiency and performance. Might need to search another way when there's tonnes of data I guess. <?php $arr = [ [1,9,3,7,5,7,5], [6,70,82,97,140], [29,38,42,56] ]; $num = 42; // number to find $rows = count($arr); // number of rows for($row = 0; $row < $rows; $row++) { // number of columns for row $row $columns = count($arr[$row]); for($col = 0; $col < $columns; $col++) { if($arr[$row][$col] == $num) { $res = [$row, $col]; break 2; } } } if(isset($res)) // [row, column] echo "$num found at row $res[0] column $res[1]"; else echo "$num not found."; ?>
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-get-single-value-from-an-array-in-php.php
How to Get Single Value from an Array in PHP
<?php // Indexed array $sports = array("Baseball", "Cricket", "Football", "Shooting"); // Associative array $cities = array("France"=>"Paris", "India"=>"Mumbai", "UK"=>"London", "USA"=>"New York"); // Multidimensional array $superheroes = array( array( "name" => "Peter Parker", "character" => "Spider-Man", ), array( "name" => "Tony Stark", "character" => "Iron-Man", ), array( "name" => "Clark Kent", "character" => "Super-Man", ) ); echo $sports[0]; // Outputs: Baseball echo "<br>"; echo $sports[1]; // Outputs: Cricket echo "<br>"; echo $cities["France"]; // Outputs: Paris echo "<br>"; echo $cities["USA"]; // Outputs: New York echo "<br>"; echo $superheroes[0]["name"]; // Outputs: Peter Parker echo "<br>"; echo $superheroes[1]["character"]; // Outputs: Iron-Man ?>
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
Get index of array? - PHP Coding Help - PHP Freaks
March 22, 2007 - I uses the function in_array to find out if a specified word appears in a specified array, if it is there I want to get the index of this word in the array. So I can use this index to set a value in array2, at the same index. Jeg bruker in_array for å sjekke om et bestemt ord finnes i en bestemt ...
🌐
TutorialKart
tutorialkart.com › php › php-access-array-elements-using-index
PHP - Access array elements using index
May 7, 2023 - PHP Access Array Elements - To access elements of array in PHP, you can use array variables followed by index which is enclosed in square brackets. The syntax is $element = $array[$index]. In this tutorial, we provide examples to understand ...