Magento 2 – How to Unserialize Array to Get Data

arraymagento2

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;

Best Answer

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"];
}