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 ™.

This snippet shows how to transform an array into a traversable object with PHP. A reminder: iterable is a type hint introduced in...

PHP

Published on 2024-04-21 • Modified on 2024-04-21

 View the code


This snippet shows how to use negative indices with PHP arrays. It is one of the breaking changes introduced by PHP 8. If an array...

PHP

Published on 2024-02-24 • Modified on 2024-02-24

 View the code


This snippet shows how to pick random keys of an array with PHP. This pickArrayKeys() is very convenient and, unlike most other ra...

PHP

Published on 2023-09-13 • Modified on 2023-09-13

 View the code


I continue my series about PHP 8.2 and the randomizer class. This time, we see how to shuffle an array thanks to the shuffleArray(...

PHP

Published on 2023-09-11 • Modified on 2023-09-11

 View the code


This snippet shows how to index a PHP array of objects by their class name. We can use the array_reduce function instead of doing ...

PHP

Published on 2023-06-17 • Modified on 2023-06-17

 View the code


This snippet shows how to order a PHP array of objects by a given property. The sort function is in the snippet class, but if we w...

PHP

Published on 2023-05-31 • Modified on 2023-05-31

 View the code


This snippet shows how to generate an array with the same keys and values with PHP. This is the first time I have tried chatGPT to...

PHP

Published on 2022-12-10 • Modified on 2022-12-10

 View the code


This snippet shows how to create a fixed-size array with PHP. We can use the SplFixedArray class. It is interesting to use this s...

PHP

Published on 2022-11-11 • Modified on 2022-11-11

 View the code


This snippet shows how to test if an array is a list with PHP. This function was introduced in PHP 8.1, before we had to do this m...

PHP

Published on 2022-09-10 • Modified on 2022-09-10

 View the code


In this snippet, we see how to filter a Doctrine collection. We have this filter() function we can call with a closure. Here I use...

Doctrine

Published on 2022-03-19 • Modified on 2022-03-19

 View the code