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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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.
Do share your doubts in the Comments section below and I’d help you out.
Please share the solution via social media with fellow developers.
Thank you.
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.
Prev
How To Convert Array Data Into Serialize Format In Magento 2
How to Apply OR Conditions To Collection In Magento 2
Next