2

I've created Magento 2 module that saves a particular configuration. Screenshot:enter image description here

Now I want to use this value in a frontend block. If I check the core_config table, the value is saved like this:

a:1:{s:18:"_1498478770604_604";a:4:{s:4:"name";s:12:"Number Pi";s:5:"image";s:23:"/media/wysiwyg/test.png";s:4:"text";s:606:"Lorum ipsum";s:20:"activation_attribute";s:1:"1";}}

How can I decode or unserialize this data? I want to turn this data into a (object) array that I can use on the frontend.

I've used the PHP function unserialize() but this didn't work. Also i've searched the Magento 2 JSON decode library, but I didn't find an answer there.

Any help would be appreciated.

4 Answers 4

-3

Use \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, In your constructor argument and set the class property: $this->scopeConfig = $scopeConfig;

Now to Get the configuration value just use

$fields =  $this->scopeConfig->getValue('yourSystemConfigPath', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$fields = unserialize($fields);
1
  • it's core php's method while magento has its own method already. you just need to use "use Magento\Framework\Serialize\SerializerInterface;" Commented Sep 1, 2021 at 4:30
3

This worked for me.

use Magento\Framework\Serialize\SerializerInterface;

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

public function jsonToArray($data)
{
    return $this->serializer->unserialize($data);
}

Hope it help. Thanks.

3
  • 1
    That's the valid answer
    – dudzio
    Commented Jul 29, 2021 at 17:27
  • 1
    use Magento\Framework\Serialize\SerializerInterface; u forgot this line Commented Aug 27, 2021 at 10:18
  • thanks @GohilRajesh, you are right. I've updated the answer.
    – fudu
    Commented Aug 31, 2021 at 3:34
1

unserialize might not work if have magento 2.2.x. Magento 2.0.x and Magento 2.1.x use default PHP serialize functionality. Starting from Magento 2.2.x it uses the JSON format by default.

What you can do is create a helper like this to get serialized value from DB that will work in any magento version

<?php

namespace Nwdthemes\ArraySerialized\Helper;

use Magento\Framework\App\ObjectManager;

/**
 * ArraySerialized helper
 */
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * Get config value
     * @param $configPath
     * @param null $store
     * @return mixed
     */
    public function getConfigValue($configPath, $store = null)
    {
        return $this->scopeConfig->getValue(
            $configPath,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $store
        );
    }

    /**
     * Get serialized config value
     * temporarily solution to get unserialized config value
     * should be deprecated in 2.3.x
     *
     * @param $configPath
     * @param null $store
     * @return mixed
     */
    public function getSerializedConfigValue($configPath, $store = null)
    {
        $value = $this->getConfigValue($configPath, $store);

        if (empty($value)) return false;

        if ($this->isSerialized($value)) {
            $unserializer = ObjectManager::getInstance()->get(\Magento\Framework\Unserialize\Unserialize::class);
        } else {
            $unserializer = ObjectManager::getInstance()->get(\Magento\Framework\Serialize\Serializer\Json::class);
        }

        return $unserializer->unserialize($value);
    }

    /**
     * Check if value is a serialized string
     *
     * @param string $value
     * @return boolean
     */
    private function isSerialized($value)
    {
        return (boolean) preg_match('/^((s|i|d|b|a|O|C):|N;)/', $value);
    }

}

public function getConfigValue() used to load any value from config table.

public function getSerializedConfigValue() is modified to automatically unserialize config data. It detect if we have serialized string or json string and use proper method to unserialize data. This code works in both 2.0.x – 2.1.x and 2.2.x versions. Starting from 2.2.4 it should be enough to use just getConfigValue().

0
<?php
namespace Rajesh\Serialization\Model;

use Magento\Framework\Serialize\SerializerInterface;

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

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

    /**
     * get Serialize Data
     *
     * @return array
     */
    public function getSerializeData()
    {
        $data = {"name":"yourvalue"}; //here serialize string
        $additionalData = $this->serializer->unserialize($data);  //here unserialize  data/array
        return $additionalData;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.