XML to Array

In terms of importing data is often set to be XML and have to pass an array in WordPress, which passes into the database. To get an array in PHP after parsing XML, you will always find a variety of threads and questions. In the following, therefore, a simple and quick solution that works great for PHP5.

// only for errors an parse
$filename = preg_replace( 
    "/\<\!\[CDATA\[(.*?)\]\]\>/ies",
    "'[CDATA]' . base64_encode('$1') . '[/CDATA]'",
    $filename 
);
// load xml file
$xml      = simplexml_load_string( $filename );
// seesaw ;-)
$json     = json_encode( $xml );
$options  = json_decode( $json, TRUE );
// see result
var_dump( $options );

// update in WordPress
update_option( 'my_settings_id', $options );

Posted

in

by