How to convert array data into Serialize format Magento 2.

Serialize array data using Magento 2 by natve SerializerInterface class.

The serialize method is used for storing or passing PHP values without losing their data type and structure.

serialize() method is used for serializing the data in Magento 2.

Use Magento\Framework\Serialize\SerializerInterface interface to create serialize data using Magento 2.

<?php
namespace Jesadiya\Serialization\Model;

use Magento\Framework\Serialize\SerializerInterface;

class DataSerialize
{
    /**
     * @var SerializerInterface
     */
    private $serializer;

    /**
     * @param SerializerInterface $serializer
     */
    public function __construct(
        SerializerInterface $serializer
    ) {
        $this->serializer = $serializer;
    }

    /**
     * set Serialize Data
     *
     * @return string
     */
    public function setSerializeData()
    {
        $data = ['code1' => 'value1'];
        $additionalData = $this->serializer->serialize($data);
        return $additionalData;
    }
}

call from a template or other class,
You can pass a value as a string, array, int or any type to convert data into serializing format.

echo $this->setSerializeData();

Output:
{“code1″:”value1”}

Check the link for, Get serailize value using Magento 2.