Need a function for wp all export to unserialize data of a specific field

1k Views Asked by At

Ι need a function to unserialize data of a specific field to export to XML File.

My serialize field is

<B2B_Sale_price><![CDATA[a:1:{i:6704;a:2:{s:13:"regular_price";s:8:"17.33871";s:10:"sale_price";s:5:"13.77";}}]]></B2B_Sale_price>

and i'd like to export only the sale_price.

This function works perfect for csv export

function data_deserialize_csv($value){$output = '';$data = unserialize($value);print_r($data);$data = reset($data);return $data['sale_price'];}

But for xml return an error PHP Error: reset() expects parameter 1 to be array, bool given on line 15 of the Functions Editor

1

There are 1 best solutions below

1
On

You can use a version of the code here to go from XML to JSON, and then to an array.

function data_deserialize_xml($value)
{
    // Parse as XML, treating the CDATA as content
    $xml = simplexml_load_string($value, null, LIBXML_NOCDATA);
    
    // Convert back and forth
    $json = json_encode($xml);
    $array = json_decode($json, TRUE);
    
    // Grab the first item
    $value = reset($array);
    
    // Same code as before
    $data = unserialize($value);
    print_r($data);
    $data = reset($data);
    return $data['sale_price'];
}