Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Adam Wathan:
Cleaning Up Form Input with Transpose
Apr 15, 2016 @ 16:50:34

Adam Wathan has a post on his site showing you how to use "transpose" functionality to clean up form input, transforming a set of arrays from submitted data back into a better structure.

Transpose is an often overlooked list operation that I first noticed in Ruby. The goal of transpose is to rotate a multidimensional array, turning the rows into columns and the columns into rows.

In his example, he adding multiple "contacts" at once. He shows the difficulties with this, the form structure and the data returned. There's several formats that could be returned but none are the correct structure to push into a model (his examples are in Laravel there's a generic array_map example too) He shows how to update the Laravel collection handling using a custom macro, transpose, to handle the array_map functionality in a more self-contained way. He includes the code to make the macro work and an example of it in use to correctly format his incoming contact data into something he can use in his collection.

tagged: transpose laravel collection array data format arraymap

Link: http://adamwathan.me/2016/04/06/cleaning-up-form-input-with-transpose/

Michelangelo van Dam:
PHP Arrays - The php array functions
Feb 08, 2016 @ 17:51:02

Michelangelo Van Dam is back with another part of his series covering the use of arrays in PHP. In previous articles he covered some of the basics including operations and associative arrays but in this new post he moves up and talks about some of the other functions you can use to manipulate them even further.

In my previous article about arrays (the basics, simple operations and associative arrays or hash maps) I have shown what arrays are and what you can do with it. Now I want to dive into real fun and explain PHP's array functions with real-world examples and how you can apply them in your day-to-day work.

He then goes through several of the PHP array functions, providing simple code snippets of them in action. To help apply it to a more "real world" situation he uses data based on a "countries" table in a database, making use of the "iso" and "printable_name" columns:

This is only three of the many methods you can use in PHP to manipulate arrays but it gives you a taste of what's there and what's to come in his future articles.

tagged: array tutorial functions arraywalk arraymap arrayintersectkey

Link: http://www.dragonbe.com/2016/02/php-arrays-php-array-functions.html

Sebastian Bergmann's Blog:
Map and Reduce in PHP
Feb 20, 2008 @ 15:31:00

In this new blog entry, Sebastian Bergmann talks about two bits of functionality that PHP has that bring it a bit closer to a full functional language - array_map and array_reduce.

Just like Python, PHP is not a full-fledged functional language, but it supports some very useful functional idioms such as map and reduce. A blog posting by Scott Moonen on Functional Python prompted me to write this posting on PHP's array_map() and array_reduce() functions that apply callback functions to arrays.

He includes examples of each (in the functional context) showing how you can map callbacks to the array values, one that adds one to each number in the array and the other that combines the values and returns the sum.

tagged: functional language arraymap arrayreduce example

Link:

Metapundit.net:
Partial function application in PHP
Feb 14, 2007 @ 13:54:00

On the Metapundit.net site, there's a new (long) entry that takes a look at one of the programming styles, functional programming, and checks into its support in PHP. Unfortunately, it's mostly a swing and a miss.

I should just get this straight right off the bat: you can't really do much functional programing in PHP. Functions are not first class citizens and the equivalent of passing functions around is passing around strings or arrays and relying on convention. No really.

He goes on by illustrating the point that PHP can do this sort of thing but only up to a point. Once you start to get into anonymous functions and moving past things like the array_map function, you start to loose a foothold. The create_function function allows for a bit more flexibility, but still doesn't fulfill the requirements needed for full support.

tagged: partial function application arraymap createfunction programming partial function application arraymap createfunction programming

Link:

Metapundit.net:
Partial function application in PHP
Feb 14, 2007 @ 13:54:00

On the Metapundit.net site, there's a new (long) entry that takes a look at one of the programming styles, functional programming, and checks into its support in PHP. Unfortunately, it's mostly a swing and a miss.

I should just get this straight right off the bat: you can't really do much functional programing in PHP. Functions are not first class citizens and the equivalent of passing functions around is passing around strings or arrays and relying on convention. No really.

He goes on by illustrating the point that PHP can do this sort of thing but only up to a point. Once you start to get into anonymous functions and moving past things like the array_map function, you start to loose a foothold. The create_function function allows for a bit more flexibility, but still doesn't fulfill the requirements needed for full support.

tagged: partial function application arraymap createfunction programming partial function application arraymap createfunction programming

Link:


Trending Topics: