PHP数组到XML的困境

时间:2011-07-22 20:03:23

标签: php xml arrays json

我试图从我的数据库中获取一些数据,然后尝试以XML和JSON显示它。在我从db中选择所有内容后,我将所有内容添加到名为$data的数组中。因此,根据我希望它显示的方式,我可以简单地json_encode()或使用一些lib(This One)来转换为XML。现在我的问题/问题是我正在尝试在数组中形成输出样式,所以我只需编码并输出它。

所以这是数组数据

  

数组(

[0] => Array
    (
        [Key1] => value1
        [Key2] => value2
        [Key3] => value3
        [Key4] => value4
    )

[1] => Array
    (
        [Key1] => value1
        [Key2] => value2
        [Key3] => value3
        [Key4] => value4
    )

[2] => Array
    (
        [Key1] => value1
        [Key2] => value2
        [Key3] => value3
        [Key4] => value4
    ) 
     

我希望它以XML和Json

的形式出现
<xml>
<result>
<key1>value1</key1>
<key2>value2</key2>
<key3>value3</key3>
<key4>value4</key4>
</result>

<result>
<key1>value1</key1>
<key2>value2</key2>
<key3>value3</key3>
<key4>value4</key4>
</result>

<result>
<key1>value1</key1>
<key2>value2</key2>
<key3>value3</key3>
<key4>value4</key4>
</result>
</xml>

现在我正在尝试找到一种解决方案,以便使用密钥result创建这些数组,所以当我将数组直接转换为json或xml时,我不必进行其他更改以使每个结果条目遵守结果标记。

有办法做到这一点吗?将每个数组添加到键result会覆盖所有条目,并仅输出最后一个条目。

由于

4 个答案:

答案 0 :(得分:3)

幸运的是,我只是为自己写这种东西......基本上你提供了一个你想要使用的元素列表,默认情况下它会使用它们的键/索引。希望这有帮助。

<?PHP

class Serializer
{   
    private static function getTabs($tabcount)
    {
        $tabs = '';
        for($i = 0; $i < $tabcount; $i++)
        {
            $tabs .= "\t";
        }
        return $tabs;
    }

    private static function asxml($arr, $elements = Array(), $tabcount = 0)
    {
        $result = '';
        $tabs = self::getTabs($tabcount);
        foreach($arr as $key => $val)
        {
            $element = isset($elements[0]) ? $elements[0] : $key;
            $result .= $tabs;
            $result .= "<" . $element . ">";
            if(!is_array($val))
                $result .= $val;
            else
            {
                $result .= "\r\n";
                $result .= self::asxml($val, array_slice($elements, 1, true), $tabcount+1);
                $result .= $tabs;
            }
            $result .= "</" . $element . ">\r\n";
        }
        return $result;
    }

    public static function toxml($arr, $root = "xml", $elements = Array())
    {
        $result = '';
        $result .= "<" . $root . ">\r\n";
        $result .= self::asxml($arr, $elements, 1); 
        $result .= "</" . $root . ">\r\n";
        return $result;
    }
}

    $arr = Array (
    0 => Array
    (
        'Key1' => 'value1',
        'Key2' => 'value2',
        'Key3' => 'value3',
        'Key4' => 'value4',
    ),

    1 => Array
    (
        'Key1' => 'value1',
        'Key2' => 'value2',
        'Key3' => 'value3',
        'Key4' => 'value4',
    ),

    2 => Array
    (
        'Key1' => 'value1',
        'Key2' => 'value2',
        'Key3' => 'value3',
        'Key4' => 'value4',
    ),
);
?>

示例1

echo Serializer::toxml($arr, "xml", array("result"));

    //output
<xml>
    <result>
        <Key1>value1</Key1>
        <Key2>value2</Key2>
        <Key3>value3</Key3>
        <Key4>value4</Key4>
    </result>

    <result>
        <Key1>value1</Key1>
        <Key2>value2</Key2>
        <Key3>value3</Key3>
        <Key4>value4</Key4>
    </result>
    <result>

        <Key1>value1</Key1>
        <Key2>value2</Key2>
        <Key3>value3</Key3>
        <Key4>value4</Key4>
    </result>
</xml>

Exmaple 2

echo Serializer::toxml($arr, "xml", array("result", "item"));

// output
<xml>
    <result>
        <item>value1</item>
        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </result>

    <result>
        <item>value1</item>
        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </result>
    <result>

        <item>value1</item>
        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </result>
</xml>

示例3

echo Serializer::toxml($arr, "xml", array(null, "item"));

// output
<xml>
    <0>
        <item>value1</item>
        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </0>

    <1>
        <item>value1</item>
        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </1>
    <2>
        <item>value1</item>

        <item>value2</item>
        <item>value3</item>
        <item>value4</item>
    </2>
</xml>

答案 1 :(得分:2)

我认为您不需要使用库。

echo("<xml>");
foreach($data as $k => $v) {
    echo("<result>");
    foreach($v as $i => $j)
        echo("<".$i.">" . $j . "</".$i.">");
    echo("</result>");
}
echo("</xml>");

答案 2 :(得分:2)

假设您的外部数组被调用$array,请使用[]语法。这将为您提供一个名为“result”的数组键,它本身就是一个索引数组。当转换为XML时,我相信(尽管尚未测试)其输出将是您正在寻找的。

$results = array('result' => array());

// Looping over the outer array gives you the inner array of keys
foreach ($array as $result) {
    // Append the array of keys to the results array
    $results['result'][] = $result;
}

print_r($results);
echo json_encode($results);

现在使用您的数组到XML库来制作XML。

答案 3 :(得分:0)

foreach($ data as $ key =&gt; $ value)                 {

                    //change false/true to 0/1
                    if(is_bool($value))
                    {
                            $value = (int) $value;
                    }

                    // no numeric keys in our xml please!
                    if (is_numeric($key))
                    {
                            // make string key...
                            $key = (singular($basenode) != $basenode) ? singular($basenode) : 'item';
                    }

                    // replace anything not alpha numeric
                    $key = preg_replace('/[^a-z_\-0-9]/i', '', $key);

                    // if there is another array found recursively call this function
                    if (is_array($value) || is_object($value))
                    {
                            $node = $structure->addChild($key);

                            // recursive call.
                            $this->to_xml($value, $node, $key);
                    }

                    else
                    {
                            // add single node.
                            $value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, "UTF-8");

                            $structure->addChild($key, $value);
                    }
            }