Php get array key by value multidimensional
Another poossible solution is based on the Show Example
ExplanationThe function `array_search()` has two arguments. The first one is the value that you want to search. The second is where the function should search. The function `array_column()` gets the values of the elements which key is `'uid'`. SummarySo you could use it as:
or, if you prefer:
The original example(by xfoxawy) can be found on the DOCS. Update Due to Vael comment I was curious, so I made a simple test to meassure the performance of the method that uses I created an array which contained 1000 arrays, the structure was like this (all data was randomized):
I ran the search test 100 times searching for different values for the name field, and then I calculated the mean time in milliseconds. Here you can see an example. Results were that the method proposed on this answer needed about 2E-7 to find the value, while the accepted answer method needed about 8E-7. Like I said before both times are pretty aceptable for an application using an array with this size. If the size grows a lot, let's say 1M elements, then this little difference will be increased too. Update II I've added a test for the method based in Update III Thanks to @mickmackusa for spotting several limitations on this method:
Note on Update III
View Discussion Improve Article Save Article View Discussion Improve Article Save Article In PHP, multidimensional array search refers to searching a value in a multilevel nested array. There are various techniques to carry out this type of search, such as iterating over nested arrays, recursive approaches and inbuilt array search functions. Iterative Approach: Example:
Output: $ --> 1 --> subject Recursive Approach: Example:
Output: $ --> school3 --> data --> name Multidimensional array search using array_search() method: Example:
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. How to Find key value in multidimensional array in PHP?Multidimensional array search using array_search() method:
The array_search() is an inbuilt function which searches for a given value related to the given array column/key. This function only returns the key index instead of a search path.
How to get keys of an array in PHP?PHP: array_keys() function
The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.
What is the difference between In_array and Array_search?The main difference between both the functions is that array_search() usually returns either key or index whereas in_array() returns TRUE or FALSE according to match found in search. Value: It specifies the value that needs to be searched in an array.
How do you check if a value exists in an associative array in PHP?The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.
|