The Secret to Get Serialize Value in Array Format in Magento 2

Serialization in Magento 2 allows converting data into a string using serialize() method to store in a database.

The reverse process is unserialization using the unserialize() method of the native Magento interface.

This method is used to get serialize value in array format in Magento 2.s

Earlier I posted the solution to convert array data into serialize format in Magento 2. The below code does exactly the opposite of what the serialize() method does.

The unserialize() method converts a serialized string back into a string, integer, float, boolean, or array data.

Method to Get Serialize Value In Array Format In Magento 2:

use Magento\Framework\Serialize\SerializerInterface;

private $serializer;

public function __construct(SerializerInterface $serializer) {
  $this->serializer = $serializer;
}

//you need to pass serialize value as a parameter in this function
public function getSerializeData($arr)
{
    $unSerializeData = $this->serializer->serialize($arr);
    return $unSerializeData;
}

That’s all for Magento 2 unserialization.

Please share the solution via social media with fellow developers.

Thank you.

Chandresh Chauhan

Article by

Chandresh Chauhan

He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.