2

my code:

<?php
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$data = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('bootsgrid_deliverydate/general/mapping_date');
print_r($data);
?>  

output:

 'a:3:{
      s:18:"_1550496532530_530";
      a:1:{s:6:"field1";s:10:"27-02-2019";}
      s:18:"_1550641096153_153";
      a:1:{s:6:"field1";s:10:"26-02-2019";}
      s:18:"_1550641100928_928";
      a:1:{s:6:"field1";s:10:"28-02-2019";}
    }'

How to get a date alone?

another output:

a:3:{s:18:"_1550664988208_208";a:2:{s:6:"field1";s:7:"9:30 AM";s:6:"field2";s:7:"2:30 AM";}s:18:"_1550735728909_909";a:2:{s:6:"field1";s:7:"3:30 AM";s:6:"field2";s:7:"6:30 AM";}s:18:"_1550735856179_179";a:2:{s:6:"field1";s:7:"6:30 AM";s:6:"field2";s:7:"9:30 AM";}}

how can i get a field1 and field2 value in same array link;

4
  • how did you get this data? is it from a response of a request?
    – fmsthird
    Commented Feb 20, 2019 at 6:49
  • its a response of serialized array @magefms Commented Feb 20, 2019 at 6:53
  • if you are using a json format post data you can use json_decode and get the date , like a:3->a:1->s:10
    – fmsthird
    Commented Feb 20, 2019 at 6:57
  • used just a print_r($data) @magefms Commented Feb 20, 2019 at 7:00

3 Answers 3

2

Try this:

<?php

//get data of an unserialized array
$serialized_data = serialize(array('_1550496532530_530',array('field1'=>'27-02-2019'),
                                '_1550641096153_153', array('field1'=>'26-02-2019'),
                                '_1550641100928_928', array('field1' => '28-02-2019')));

echo  $serialized_data . '<br>';
// Unserialize the data
$data = unserialize($serialized_data);

// Show the unserialized data;
echo "<pre>";
print_r($data);

for ($i = 1; $i < count($data); $i++) {
    echo '<pre>';
    echo $data[$i]["field1"];
    $i = $i+1;
}  

or this one using foreach

$data = 'a:3:{s:18:"_1550496532530_530";a:1:{s:6:"field1";s:10:"27-02-2019";}s:18:"_1550641096153_153";a:1:{s:6:"field1";s:10:"26-02-2019";}s:18:"_1550641100928_928";a:1:{s:6:"field1";s:10:"28-02-2019";}}';

// Show the unserialized data;
$postData = unserialize($data);
echo "<pre>";
print_r($postData);

foreach ($postData as $date) {
    echo '<pre>';
    echo $date["field1"];
}
13
  • i wil check and update u Commented Feb 20, 2019 at 7:22
  • Please note that my answer is assuming that your $data is the unserialized data
    – fmsthird
    Commented Feb 20, 2019 at 7:30
  • anyway I will update it and add the unserialize
    – fmsthird
    Commented Feb 20, 2019 at 7:30
  • its not working Commented Feb 20, 2019 at 9:04
  • try this updated one
    – fmsthird
    Commented Feb 20, 2019 at 9:39
2

The "Magento way" would be to create a Serializer object (depending on the way your data is serialized, for example Json or Serialize) and call the unserialize method there. For example like this (of course you can inject the Serializer object in your constructor depending on your context):

$serializedData = 'a:3:{s:18:"_1550496532530_530";a:1:{s:6:"field1";s:10:"27-02-2019";}s:18:"_1550641096153_153";a:1:{s:6:"field1";s:10:"26-02-2019";}s:18:"_1550641100928_928";a:1:{s:6:"field1";s:10:"28-02-2019";}}';

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$serializer = $objectManager->get('\Magento\Framework\Serialize\Serializer\Serialize');
$dataArray = $serializer->unserialize($serializedData);
print_r($dataArray);

Anyway the result is the same as just using the php unserialize method.

Update with complete working code based on the updated code from the question:

anyway if your data is not serialized in a correct way (for example whatever formatting whitspace inside the data) the deserialize will fail!

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$data = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('bootsgrid_deliverydate/general/mapping_date');
print_r($data);

//deserialize...
$serializer = $objectManager->get('\Magento\Framework\Serialize\Serializer\Serialize');
$dataArray = $serializer->unserialize($data);
print_r($dataArray);
5
  • this one is not working Commented Feb 20, 2019 at 9:04
  • can you paste your complete code which is not working please? If I just copy the 5 lines into a controller I get the array.
    – HelgeB
    Commented Feb 20, 2019 at 9:10
  • If you want to access a single value, you have to access it by key, for example echo $dataArray['_1550496532530_530']['field1']; Or iterate over the array, however you have an deserialized array and can do whatever you need with it.
    – HelgeB
    Commented Feb 20, 2019 at 9:18
  • i edit my code in above question Commented Feb 20, 2019 at 9:26
  • so, where is the problem? Take line 3 and line 4 from my code, replace ` $serializedData with $data and you are done, you will have an array - but I'll update my response and do that for you :-)
    – HelgeB
    Commented Feb 20, 2019 at 9:37
0

Try this:

$data = 'a:3:{s:18:"_1550496532530_530";a:1:{s:6:"field1";s:10:"27-02-2019";}s:18:"_1550641096153_153";a:1:{s:6:"field1";s:10:"26-02-2019";}s:18:"_1550641100928_928";a:1:{s:6:"field1";s:10:"28-02-2019";}}';

$dataUnserialized = unserialize($data);

Here the result of var_dump($dataUnserialized):

result

so you can access your data as a simple array, like:

$date = $dataUnserialized['_1550496532530_530']['field1'];
2
  • its explode first date because of this one ['_1550496532530_530'], how to get a all the dates @Siarhey Commented Feb 20, 2019 at 9:03
  • @divyasekar You can use the foreach or other construct provided by php. Commented Feb 20, 2019 at 9:17

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.