Shuffling an array with PHP and the randomizer class

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

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() function.



namespace App\Controller\Snippet;

 * I am using a PHP trait to isolate each snippet in a file.
 * This code should be called from a Symfony controller extending AbstractController (as of Symfony 4.2)
 * or Symfony\Bundle\FrameworkBundle\Controller\Controller (Symfony <= 4.1).
 * Services are injected in the main controller constructor.
trait Snippet272Trait
    public function snippet272(): void
        $r = new \Random\Randomizer();
        $array = ['a', 'b', 'c', 'd', 'foo', 'bar'];

        echo PHP_EOL;

        // That's it! 😁

 Run this snippet  More on Stackoverflow   Read the doc  Random snippet

  Work with me!