PHP数组到XML的两难选择

PHP数组到XML的两难选择,php,xml,arrays,json,Php,Xml,Arrays,Json,我试图从数据库中获取一些数据,然后尝试用XML和JSON显示它。从数据库中选择所有内容后,我将所有内容添加到名为$data的数组中。因此,根据我希望它的显示方式,我可以简单地json\u encode()it或使用一些lib()将其转换为XML。现在我的问题是,我试图在数组中形成输出样式,所以我所要做的就是编码并输出它 这就是数组数据 排列( ) 我希望它以XML和Json的形式出现 <xml> <result> <key1>value1</key1&g

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

这就是数组数据

排列(

)

我希望它以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>

价值1
价值2
价值3
价值4
价值1
价值2
价值3
价值4
价值1
价值2
价值3
价值4
现在,我正试图找到一种解决方案,使这些数组具有一个键
result
,因此,当我将数组直接转换为json或xml时,我不必进行额外的更改,以使每个结果项都符合result标记

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


谢谢

我想你不需要用图书馆来做这个

echo("<xml>");
foreach($data as $k => $v) {
    echo("<result>");
    foreach($v as $i => $j)
        echo("<".$i.">" . $j . "</".$i.">");
    echo("</result>");
}
echo("</xml>");
echo(“”);
foreach($k=>v的数据){
回声(“”);
foreach($v为$i=>$j)
回声(“$j.”);
回声(“”);
}
回声(“”);

假设外部数组名为
$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。

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

<?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',
    ),
);
?>
$val)
{
$element=isset($elements[0])?$elements[0]:$key;
$result.=$tabs;
$result.=”;
如果(!is_数组($val))
$result.=$val;
其他的
{
$result.=“\r\n”;
$result.=self::asxml($val,array_slice($elements,1,true),$tabcount+1);
$result.=$tabs;
}
$result.=“\r\n”;
}
返回$result;
}
公共静态函数toxml($arr,$root=“xml”,$elements=Array())
{
$result='';
$result.=“\r\n”;
$result.=self::asxml($arr,$elements,1);
$result.=“\r\n”;
返回$result;
}
}
$arr=数组(
0=>数组
(
'Key1'=>'value1',
'Key2'=>'value2',
'键3'=>'值3',
'Key4'=>'value4',
),
1=>数组
(
'Key1'=>'value1',
'Key2'=>'value2',
'键3'=>'值3',
'Key4'=>'value4',
),
2=>数组
(
'Key1'=>'value1',
'Key2'=>'value2',
'键3'=>'值3',
'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>
echo序列化程序::toxml($arr,“xml”,数组(“result”); //输出 价值1 价值2 价值3 价值4 价值1 价值2 价值3 价值4 价值1 价值2 价值3 价值4 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>
echo序列化程序::toxml($arr,“xml”,数组(“结果”,“项”); //输出 价值1 价值2 价值3 价值4 价值1 价值2 价值3 价值4 价值1 价值2 价值3 价值4 示例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>
echo序列化程序::toxml($arr,“xml”,数组(null,“item”); //输出 价值1 价值2 价值3 价值4 价值1 价值2 价值3 价值4 价值1 价值2 价值3 价值4 foreach($key=>$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);
                    }
            }

您好,我知道我可以这样做,但是库也支持嵌套数组..但是,这对我没有帮助,因为当我用json编码时,它只显示条目,而不使用结果标记标记条目,它只显示条目..–您希望json的对象结构与您为XML列出的完全相同吗?是的..基本上我只想在该数组中构造输出,根据需要(xml或json),我只想将数组编码为xml或json
                    //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);
                    }
            }