Php associative array get all values10/11/2023 While array_column() works for my scenario (if I were using PHP 5.5+ instead of PHP 5.3), it wouldn't work for the modified solution above (Hence the edit). ![]() The following solution worked in this scenario: $result = array_map(function($v), $people) If a later version of PHP adds this functionality, please state that in your answer. PHP: Return value associated with specific key in an array of associative arrays. Multidimensional associative array is often used to store data in group relation. Associative array stores the data in the form of key and value pairs where the key can be an integer or string. Something like this involves arrays, keys, and values, so searching for that type of stuff tends to constantly point me to array_keys() or array_values(), neither of which is what I want.įor the particular application I'm using, the ordering of the values when returned does not matter.Īlso, the version of PHP I'm using is 5.3. PHP Multidimensional array is used to store an array in contrast to constant values. Get the value of an associative array if the key or part of the key is matched. I've tried searching around for this, but I might not be using the right keywords. PHP associative array get values in an array. Yes, I know, I'm only saving myself 10 to 20 lines of code by not writing the function I need, but in a framework that uses tons of files, having to constantly include a library to do something this simple tends to get a little tedious. Given that PHP is littered with a plethora of quirky functions that do weird things, I'm wondering if something like this already exists. How can i get (echo) all names and corresponding ages in a single instance (like foreach value as value ) The array may have more data than shown here. I have an array ages array ('Peter'>32, 'Quagmire'>30, 'Joe'>34). ![]() However, I doubt that I'm the first person to need this kind of functionality. get all values from php associative array. This is how associative arrays work in php, if you specify the key as you did, you don't get a numeric key. I just need to do: $names = array_keyed_values($people, 'name') Your associative array here has string keys. * array The Array of Keyed Values within the Array of Arrays.įunction array_keyed_values($array, $key)Ĭool, I've officially solved my problem. * string $key The Key within each Associative Array to retrieve the Value * array $array The Array of Associative Arrays. * Returns an Array of Values paired with the specified $key Need the values and not Enum instances I've written a Composer package for this, othyn/php-enum-enhancements, as the UnitEnum::cases() method wasn't what I was looking for, as that returns an array of MySuperCoolEnum instances instead of the underlying values as their raw type, which is what I wanted. to many it might look like a) works, but actually b) is happening, when you needed c) all along. ![]() ![]() I know that I could do this: $names = array() īut let's say that I'm lazy, so I create a function for it instead: /** you need a & - the other way (accepted) is sort of a subtle 'hack' - i.e the accepted answer by Rikesh works indirectly for reasons that are not obvious, i.e. I do not need the 'user_email' key/value pair.Let's say that I have an array like this: $people = array(Īnd I want to know the names of all of these people. These are the fields I'd like to get from the $post_data array Finally, you can use arrayvalues() to get all the values of the array, then take the first. I'd then like to create a new array named $clean_data with these key/value pairs.īelow is my failed attempt at looping through the $post_data array and pulling out the matches based on the $my_fields array. Only where the key matches a provided list $my_fields.Īnd create a new array with only the matched data.įor example, from $post_data I'd like to grab the key/value pairs for first_name, last_name, and title while ignoring user_email. Grab key/value pairs from an array $post_data.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |