多维数组存入ini文件

Multidimensional array into a ini file

我有一些简单的 XML 代码需要先从服务中读取(已排序),它看起来像这样:

<?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>

然后我有这段代码可以用JSON阅读它:

$xmlfile = file_get_contents($path);
$ob= simplexml_load_string($xmlfile);
$json  = json_encode($ob);
$configData = json_decode($json, true);

带有 var_dump($configData); 的输出给出:

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

我在另一个线程中发现了一个我认为可以使用但似乎无法解析多维数组的函数?

function write_ini_file($assoc_arr, $path, $has_sections=FALSE) { 
   $content = ""; 
   if ($has_sections) { 
    foreach ($assoc_arr as $key=>$elem) { 
        $content .= "[".$key."]\n"; 
        foreach ($elem as $key2=>$elem2) { 
            if(is_array($elem2)) 
            { 
                for($i=0;$i<count($elem2);$i++) 
                { 
                    $content .= $key2."[] = \"".$elem2[$i]."\"\n"; 
                } 
            } 
            else if($elem2=="") $content .= $key2." = \n"; 
            else $content .= $key2." = \"".$elem2."\"\n"; 
        } 
    } 
  } 
  else { 
    foreach ($assoc_arr as $key=>$elem) { 
        if(is_array($elem)) 
        { 
            for($i=0;$i<count($elem);$i++) 
            { 
                $content .= $key."[] = \"".$elem[$i]."\"\n"; 
            } 
        } 
        else if($elem=="") $content .= $key." = \n"; 
        else $content .= $key." = \"".$elem."\"\n"; 
    } 
  }


  if (!$handle = fopen($path, 'w')) { 
    return false; 
  }

  $success = fwrite($handle, $content);
  fclose($handle); 

  return $success;  }

  write_ini_file($configData, './data.ini', true);

任何人都可以帮助弄清楚如何管理函数以便我可以将数组数据保存在 ini 文件中吗?

更新:只想分享我找到的适合我的目的的代码。

<?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[] = "\r\n"."[$section-$key]";
        foreach($values as $var => $val) {
            $ini[] = "$var = \"$val\"";
        }
    }
}

 $ini = implode("\r\n", $ini);
 nl2br($ini);
 file_put_contents('./data.ini', $ini);

 ?>

在我的 INI 文件中提供像这样的漂亮格式:

[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 归功于 AbraCadaver

所以这适用于结构类似于您的数组:

选项 1

foreach($configData as $section => $array) {
    $ini[] = "[$section]";
    foreach($array as $key => $values) {
        foreach($values as $var => $val) {
            $ini[] = "{$key}[$var] = \"$val\"";
        }
    }
}
$ini = implode("\n", $ini);

产生 ini 输出:

[book]
0[id] = "1"
0[title] = "Murder1"
0[author] = "John doe"
0[heading] = "Reminder to read"
0[body] = "A very exciting book etc"
1[id] = "2"
1[title] = "Murder2"
1[author] = "Jane doe"
1[heading] = "Reminder to read to"
1[body] = "Also a very exciting book"

使用部分解析它:

$array = parse_ini_string($ini, true);

生成与开始时相同的数组:

Array
(
    [book] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [title] => Murder1
                    [author] => John doe
                    [heading] => Reminder to read
                    [body] => A very exciting book etc
                )

            [1] => Array
                (
                    [id] => 2
                    [title] => Murder2
                    [author] => Jane doe
                    [heading] => Reminder to read to
                    [body] => Also a very exciting book
                )
        )
)

选项 2

将最里面的行更改为:

$ini[] = "{$var}[$key] = \"$val\"";

产生更传统的 ini 输出:

[book]
id[0] = "1"
title[0] = "Murder1"
author[0] = "John doe"
heading[0] = "Reminder to read"
body[0] = "A very exciting book etc"
id[1] = "2"
title[1] = "Murder2"
author[1] = "Jane doe"
heading[1] = "Reminder to read to"
body[1] = "Also a very exciting book"

但是读回数组时,结构不同:

Array
(
    [book] => Array
        (
            [id] => Array
                (
                    [0] => 1
                    [1] => 2
                )

            [title] => Array
                (
                    [0] => Murder1
                    [1] => Murder2
                )

            [author] => Array
                (
                    [0] => John doe
                    [1] => Jane doe
                )

            [heading] => Array
                (
                    [0] => Reminder to read
                    [1] => Reminder to read to
                )

            [body] => Array
                (
                    [0] => A very exciting book etc
                    [1] => Also a very exciting book
                )
        )
)

选项 3

根据您的评论:

foreach($configData as $section => $array) {
    foreach($array as $key => $values) {
        $ini[] = "[$section-$key]";
        foreach($values as $var => $val) {
            $ini[] = "$var = \"$val\"";
        }
    }
}
$ini = implode("\n", $ini);

产生一个 ini 部分,该部分基于由子数组的键附加的数组索引,不会解析为原始数组:

[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"

选项 4

如果不存在像 book 这样的多个层次结构,则另一种选择是:

foreach($configData as $section => $array) {
    foreach($array as $key => $values) {
        $ini[] = "[$key]";
        foreach($values as $var => $val) {
            $ini[] = "$var = \"$val\"";
        }
    }
}
echo $ini = implode("\n", $ini);

将生成一个 ini,其中带有编号的部分将解析为原始数组:

[0]
id = "1"
title = "Murder1"
author = "John doe"
heading = "Reminder to read"
body = "A very exciting book etc"
[1]
id = "2"
title = "Murder2"
author = "Jane doe"
heading = "Reminder to read to"
body = "Also a very exciting book"

无论如何,写入文件:

file_put_contents('./data.ini', $ini);