Php array map and filter
Sorry for the vague title, I had trouble summarizing this question in one sentence (I'm open to suggestions or edits). Show I have a 2 dimensional associative array with an array of 2 numbers for each key. Like this:
I'm wondering if there is a way to use array_map() or array_filter() to return an array that has each key and the first number in each of the value arrays, like this:
I do not wish to create a new array by using a loop or anything like that, I'd like to do the conversion on the fly as an argument to a function, if you know what I mean. I could write my own function to achieve this but I'm interested in knowing if it can be done with array_map() or array_filter(). I tried using various combinations of array_merge(), array_keys() and array_values() with no success. I thank you in advance for any help you may provide.
Edd Mann DeveloperOver the past couple of years I have transitioned from boilerplate-heavy, imperative code, to begin using the more intuitive Below you will find the initial collection of user entires we wish to process. The objective is to create a function which returns only the users names, excluding however, the one associated to a supplied id.
Imperative ApproachLooking at this problem with an imperative mindset leads us to iterating through the collection, adding names to a temporary collection which match the desired predicate. This boilerplate code is sprinkled throughout many code-bases, and although correct, I feel can be improved upon in its intent and expression.
Mapping and FilteringThe first higher-order functions we shall be using to tackle this problem are the
ReducingThe solution above is a significant improvement in my
opinion, but can be made even better with the introduction of
Again sadly PHP’s syntax leaves a lot to be desired in regard to declaration of lambda functions and immutable arrays. However, again the intent of the code to me has been significantly improved from the original imperative solution. How to filter in array PHP?To filter an array in PHP, use the array_filter() method. The array_filter() takes an array and filter function and returns the filtered array. The filter function is a custom user-defined function with its logic and based on that, it filters out the array values and returns them.
What is array map in PHP?Definition and Usage. The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.
What exactly is the the difference between array_map Array_walk and array_filter?The resulting array of array_map has the same length as that of the largest input array; array_walk does not return an array but at the same time it cannot alter the number of elements of original array; array_filter picks only a subset of the elements of the array according to a filtering function.
Does array map preserve keys?The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys.
|