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.

-1

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);
0
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

This worked for me.

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

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

Hope it help. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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