In PHP, how do I serialize arrays with Composer?

In PHP, you can serialize arrays using the Symfony Serializer component, which allows for robust data serialization.

This example demonstrates how to serialize an array in PHP by utilizing the Composer package symfony/serializer.
Keywords: PHP, Serialization, Arrays, Symfony, Composer
<?php // First, install the Symfony Serializer component via Composer // composer require symfony/serializer require 'vendor/autoload.php'; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; // Create a new instance of the serializer with encoders $serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); // Sample array to serialize $data = [ 'name' => 'John Doe', 'age' => 30, 'email' => 'john@example.com' ]; // Serialize the array $jsonContent = $serializer->serialize($data, 'json'); echo $jsonContent; // Outputs serialized JSON format ?>

Keywords: PHP Serialization Arrays Symfony Composer