fokicode.blogg.se

Php associative array get all values
Php associative array get all values









php associative array get all values

php associative array get all values

The following solution worked in this scenario: $result = array_map(function($v), $people) Otherwise, all the keys from the array are returned. If the optional searchvalue is specified, then only the keys for that value are returned. If a later version of PHP adds this functionality, please state that in your answer. arraykeys() returns the keys, numeric and string, from the array. 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. Assuming keys in the sub arrays are unique you can. I've tried searching around for this, but I might not be using the right keywords.

#Php associative array get all values code

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.

php associative array get all values

Given that PHP is littered with a plethora of quirky functions that do weird things, I'm wondering if something like this already exists. I have a multidimensionalarray and I'm trying to output all values with a certain key. This is how associative arrays work in php, if you specify the key as you did, you don't get a numeric key.

php associative array get all values

However, I doubt that I'm the first person to need this kind of functionality. So I've been trying to figure this out for a while now and seem to be stuck. Your associative array here has string keys. I just need to do: $names = array_keyed_values($people, 'name') * 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 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: /** PS: I already found this example, but I was expecting something from php which would do something like getAssociativeDataByIndex($array, $index) for example.Let's say that I have an array like this: $people = array(Īnd I want to know the names of all of these people. I know that I can do a for/foreach loop together with a counter, but I'm just curious. The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. How can I get an array of values from an associative array Associate array Example: Array ( 0 > Array ( 0 > 1 1 > 2 2 > 3 ) 1 > Array ( 0 > 4 1 > 5 2 > 6 ) 2 > Array ( 0 > 7 ) ) Desired Output. Get the value of an associative array if the key or part of the key is matched. The key function helped me and is very simple. The syntax for accessing elements in an associative array is arrkey. Is there any function which can return, for example, the 3rd element of an associative array without any loop? (and without copying the array) PHP associative array get values in an array. cell is an associative array, not an object.











Php associative array get all values