【问题标题】:Multidimensional array into a ini file多维数组到ini文件
【发布时间】:2017-07-04 19:46:54
【问题描述】:

我需要先从服务中读取一些纯 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"

感谢AbraCadaver

【问题讨论】:

  • 为什么是ini文件而不是JSON?如果必须是ini文件,它应该是什么样的?
  • 嗨,我要服务的应用程序无法处理 XML,我必须在 ini 文件中显示数据。 IT 应该有像 [book] id=1 ef 这样的部分
  • book 下有 2 个数组,每个数组都有一个 id,因此在解析时,[book] id=2 将覆盖 id=1。我提供了 2 个选项,如果您需要其他内容,请提供一些反馈。

标签: php arrays multidimensional-array ini


【解决方案1】:

所以这适用于像你这样结构的数组:

选项 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);

【讨论】:

  • 您好,非常感谢您的反馈。我可能很清楚我希望输出是什么。它应该像一个传统的 INI 文件,例如:[Book] id=1 [Book] id=2 并且每个新部分和名称:值都在新行中,例如 php.ini
  • 这是不可能的,php.ini 也不是这样。您不能有 2 个 [book] 部分,只有 1 个,并且您不能在 [book] 下有 2 个 id。测试一下就知道了。
  • 好的,如果我需要搜索某个部分,我如何将它传递到一个 ini 文件中以使每个部分都独一无二? [Book-1] 标题=文本 [Book-2] 标题=文本
  • 这就是我得到的 4 个选项。
  • 再次感谢所有选项,我发现选项 3 可以解决问题。然而。你能用换行符得到输出吗?
猜你喜欢
  • 2011-11-20
  • 2021-09-18
  • 2013-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多