使用parse_ini_file将ini转换为所需格式的数组 - 无法获得所需的输出

时间:2014-04-11 16:07:36

标签: php arrays arraylist

我在包含演示者信息的查询中提取了数据。它的格式如下:

[Doe, John undefined]
middle_name = E.
department = Psychology
institution = Special State University
city = Anywhere
state = Texas
country = USA
office_phone = 123-456-7891
cell_phone = 123-789-4578
email_address = John_Doe@there.edu
website = www.johndoe.edu

[Doe, Jane undefined]
middle_name = 
department = Political Science
institution = Special University
city = anywhere
state = Indiana
country = USA
office_phone = 123-456-7891
cell_phone = 568-456-4589
email_address = janedoe@here.edu
website = 

我真的需要它显示如下:

John Doe,特殊州立大学心理学

Jane Doe,特殊大学政治学 (每个演示者在一个单独的行上)

我试过这个:

$cleanpresenters = parse_ini_file($data['presenters']);

并在错误日志中生成此内容:

 parse_ini_file([Doe, John undefined]\nmiddle_name = E.\ndepartment = Psychology\ninstitution = Special State University\ncity = Anywhere\nstate = Texas\ncountry = USA\noffice_phone = 123-456-7891\ncell_phone = \nemail_address = John_Doe@there.edu\nwebsite = \n\n): failed to open stream: File name too long in -File path name.proposalpdf.php on line 21, referer: Filepath name

我几乎是一个PHP新手,不知道接下来要做什么来修复错误并让它看起来像我想要的。可能只有一个演示者,或者可能有几个,因此解决方案需要循环,直到它遍历所有演示者。任何帮助,将不胜感激。

更新: 更改为parse_ini_string并回显数组,我得到了这个:

Array
(
    [middle_name] => 
    [department] => Political Science
    [institution] => Special University
    [city] => Anywhere
    [state] => Indiana
    [country] => USA
    [office_phone] => 123-456-7891
    [cell_phone] => 568-456-4589
    [email_address] => janedoe@here.edu
    [website] => 
) 

到目前为止,这么好。我现在如何将其转换为我想要的格式?

1 个答案:

答案 0 :(得分:0)

我根据研究和其他人编写的例子来弄清楚。

 //convert presenter data from array to string  
    $cleanpresenters = parse_ini_string($data['presenters'],TRUE);
    foreach ($cleanpresenters as $name=>$val1) 
    {
            $name = trim($name);
            $presenters[] = $name;
            $convertedPresenters .= "$name, {$val1['department']}, {$val1['institution']}\n";
    };

以上就是诀窍。每位演示者在一行上打印名称,部门,机构。感谢您提供正确方向的提示和要点。