Get key value from associative array php
If I have: Show
how do I get the string asked Nov 4, 2010 at 10:36
1 You don't. Your array doesn't have a key
This all depends on what it is exactly you want to do. The fact is, answered Nov 4, 2010 at 10:43
deceze♦deceze 497k81 gold badges718 silver badges865 bronze badges 0
Which will output:
answered Nov 4, 2010 at 10:38
kennytmkennytm 497k100 gold badges1061 silver badges994 bronze badges 0 If you only plan to work with one key in particular, you may accomplish this with a single line without having to store an array for all of the keys:
answered Feb 6, 2015 at 22:40
StarCrashrStarCrashr 4264 silver badges11 bronze badges 3 Or if you need it in a loop
answered Nov 4, 2010 at 10:42
DeckoDecko 17.6k2 gold badges26 silver badges39 bronze badges 2
answered Nov 4, 2010 at 10:44
Alex PliutauAlex Pliutau 20.8k26 gold badges108 silver badges142 bronze badges You might do it this way:
Haven't tryed the code, but i'm pretty sure it will work. Good luck! answered Nov 4, 2010 at 12:27
If it is the first element, i.e.
If it is the second element, i.e.
I think this method is should be used when required element is the first, second or at most third element of the array. For other cases, loops should be used otherwise code readability decreases. answered Nov 6, 2019 at 12:00
VivekPVivekP 711 silver badge6 bronze badges The key function helped me and is very simple:
Example:
The above example will output:
Alfie 2,2862 gold badges26 silver badges43 bronze badges answered Jul 29, 2014 at 15:52
1 One more example: Get the most frequent occurrence(s) in an array: PHP >= 7.3:
If you have the key, you can easily query the value (= the frequency). answered Apr 21, 2021 at 16:32
qräbnöqräbnö 2,38925 silver badges32 bronze badges just posting another solution for those that array_keys() is not working
answered Oct 13, 2021 at 13:25
Expanding on Ram Dane's answer, the key function is an alternative way to get the key of the current index of the array. You can create the following function,
answered Feb 1, 2016 at 19:25
AurovrataAurovrata 1,76223 silver badges42 bronze badges How get key from value in array in PHP?If you have a value and want to find the key, use array_search() like this: $arr = array ('first' => 'a', 'second' => 'b', ); $key = array_search ('a', $arr); $key will now contain the key for value 'a' (that is, 'first' ).
How do you iterate through an associative array in PHP?The foreach() method is used to loop through the elements in an indexed or associative array. It can also be used to iterate over objects. This allows you to run blocks of code for each element.
How get key of multidimensional array in PHP?Retrieving Values: We can retrieve the value of multidimensional array using the following method:. Using key: We can use key of the associative array to directly retrieve the data value. ... . Using foreach loop: We can use foreach loop to retrieve value of each key associated inside the multidimensional associative array.. How do you find array keys?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. Specified array.
|