it's easy to let arrays get away on you and wind up writing complex, hard-to-test foreach loops to modify or filter your data. php's array_map(), array_filter() and array_reduce() functions allow us to handle arrays in a clean, concise and powerful way by letting us use ideas from https://www.infoworld.com/article/3613715/what-is-functional-programming-a-practical-guide.html.

here we assign our doubling function to the variable $double and then pass that variable as an argument to array_map(): $double = function($element) { return $element * 2; }; $result = array_map($double, $testArray); Enter fullscreen mode

Exit fullscreen mode #callbacks-with-extra-arguments callbacks with extra arguments so far, all of the callable functions that we have passed to array_map() have only taken one argument: the element of the array.

if we run this snippet, we will get: Array ( => Array ([name] => Used album [price] => $5.00 [country] => US )
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners