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/


Trending Topics: