Code snippets

You will find here 21 code snippets I find useful so I don't have to Google ™ or Stackoverflow ™ them when I need. There are also code snippets I always forget! 😁 Sometimes, related links to the code will be available. The main difference with other random snippets you will find on the web is that in most cases (for the Symfony related ones) you will be able to run them directly from this website to check the output so you can verify it works as expected ™.

In this snippet, we see how to get the first item of an array with PHP. This is a classic snippet every PHP developer had to use o...

PHP

Published on 2022-01-21 • Modified on 2022-01-21

 View the code


This snippet shows how to create to a "n" dimensions matrix with PHP. In the previous snippet, we saw how to create a two dimensi...

PHP

Published on 2022-01-01 • Modified on 2022-01-01

 View the code


In this snippet, we see how to create a two dimensions array with PHP. Of course, we can make loops with the range() function but ...

PHP

Published on 2021-12-30 • Modified on 2021-12-30

 View the code


In this snippet, we see how to validate array keys type and presence with the Symfony validator component. This is straightforward...

Symfony

Published on 2021-08-07 • Modified on 2021-08-07

 View the code


In this snippet, we will see how to extract a result from an array using the array_reduce PHP function and a closure. I get all th...

PHP

Published on 2020-07-11 • Modified on 2020-07-11

 View the code


In this snippet, we will see how to loop on JavaScript arrays. I will go straight to the point and show my preferred ways, the fir...

JavaScript

Published on 2020-06-19 • Modified on 2020-06-19

 View the code


This is the kind of thing that it's easy to do with PHP. We can use unset($arr[$idx]); (and keys and preserved). But in JavaScript...

JavaScript

Published on 2020-03-07 • Modified on 2020-03-07

 View the code


The function is not explode like in PHP. Instead, we can use the split filter (that internally uses the explode or str_split PHP f...

Twig

Published on 2020-02-10 • Modified on 2020-02-10

 View the code


In this snippet, we will see how to sort a PHP array by descending numerical value. That the type of code I sometimes forget and w...

PHP

Published on 2020-02-07 • Modified on 2020-02-07

 View the code


This is how to modify all values of a PHP array with an anonymous function (closure). Note that when using array_map, the closure ...

PHP

Published on 2019-11-03 • Modified on 2019-11-03

 View the code