0

I want to save some data which is in array form and used this to save array but it will save Array keyword in this How to save array values with this.

$this->configWriter->save('my/path/whatever1', $newdata, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);

2
  • Could you please show sample data in the array. Commented Sep 9, 2020 at 14:49
  • Prits, i thing you have do json_encode and save in core_config table. Commented Sep 9, 2020 at 15:38

1 Answer 1

1

In order to save array in config database you can convert array to serialized string.

Inject Serializer Class in your constructor.

public function __construct( ...,
\Magento\Framework\Serialize\SerializerInterface $serializer) {
  /*** You other code ***/
  $this->serializer = $serializer;
}

Use the serialize() to convert array to serialized string.

$serializedData = $this->serializer->serialize($newdata);
$this->configWriter->save('my/path/whatever1', $serializedData, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);

You can convert the serialized string back to array wherever you wanna use the value as

$arrData = $this->serializer->unserialize($serializedData);

Hope it was helpful.

Thanks

2
  • Thanks for the response. How to save the data with this data as an array value not want to log that ? @Abdul Commented Sep 9, 2020 at 14:30
  • Please find the updated answer. Commented Sep 9, 2020 at 15:45

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.