I have some plain XML code that I need to first read from a service (got that sorted) and it looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<bookcase>
<book>
<id>1</id>
<title>Murder1</title>
<author>John doe</author>
<heading>Reminder to read</heading>
<body>A very exciting book etc</body>
</book>
<book>
<id>2</id>
<title>Murder2</title>
<author>Jane doe</author>
<heading>Reminder to read to</heading>
<body>Also a very exciting book</body>
</book>
</bookcase>
Then I have this code to read it with JSON:
$xmlfile = file_get_contents($path);
$ob= simplexml_load_string($xmlfile);
$json = json_encode($ob);
$configData = json_decode($json, true);
Output with var_dump($configData);
gives:
array(1) { ["book"]=> array(2) {
[0]=> array(5) {
["id"]=> string(1) "1"
["title"]=> string(7) "Murder1"
["author"]=> string(8) "John doe"
["heading"]=> string(16) "Reminder to read"
["body"]=> string(24) "A very exciting book etc" }
[1]=> array(5) {
["id"]=> string(1) "2"
["title"]=> string(7) "Murder2"
["author"]=> string(8) "Jane doe"
["heading"]=> string(19) "Reminder to read to"
["body"]=> string(25) "Also a very exciting book" }
}
}
I found in another thread a function I thought that I could use but it seems not to be able to parse arrays that are multidimensional?
function write_ini_file($assoc_arr, $path, $has_sections=FALSE) {
$content = "";
if ($has_sections) {
foreach ($assoc_arr as $key=>$elem) {
$content .= "[".$key."]
";
foreach ($elem as $key2=>$elem2) {
if(is_array($elem2))
{
for($i=0;$i<count($elem2);$i++)
{
$content .= $key2."[] = \"".$elem2[$i]."\"
";
}
}
else if($elem2=="") $content .= $key2." =
";
else $content .= $key2." = \"".$elem2."\"
";
}
}
}
else {
foreach ($assoc_arr as $key=>$elem) {
if(is_array($elem))
{
for($i=0;$i<count($elem);$i++)
{
$content .= $key."[] = \"".$elem[$i]."\"
";
}
}
else if($elem=="") $content .= $key." =
";
else $content .= $key." = \"".$elem."\"
";
}
}
if (!$handle = fopen($path, 'w')) {
return false;
}
$success = fwrite($handle, $content);
fclose($handle);
return $success; }
write_ini_file($configData, './data.ini', true);
Can anyone help figure out how to manage the function so I can save the array data in an ini file?
Update: Just want to share the code I found working for my purpose.
<?php
$path = ".\data.xml";
$xmlfile = file_get_contents($path);
$ob= simplexml_load_string($xmlfile);
$json = json_encode($ob);
$configData = json_decode($json, true);
foreach($configData as $section => $array) {
foreach($array as $key => $values) {
$ini[] = "
"."[$section-$key]";
foreach($values as $var => $val) {
$ini[] = "$var = \"$val\"";
}
}
}
$ini = implode("
", $ini);
nl2br($ini);
file_put_contents('./data.ini', $ini);
?>
Gives a nice formatting like this into my INI-File:
[book-0]
id = "1"
title = "Murder1"
author = "John doe"
heading = "Reminder to read"
body = "A very exciting book etc"
[book-1]
id = "2"
title = "Murder2"
author = "Jane doe"
heading = "Reminder to read to"
body = "Also a very exciting book"
Al credits to AbraCadaver