PHP Array到XML的难题

问题描述:

我想从我的数据库中获取一些数据,然后尝试在XML和JSON中显示它。从db中选择所有内容后,我将所有内容添加到名为$data的数组中。因此,根据我希望显示的方式,我可以简单地使用json_encode()或使用某些库(This One)转换为XML。现在我的问题是,我正在尝试在数组中形成输出样式,所以我必须对它进行编码并输出。PHP Array到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> 

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

有没有办法做到这一点?将所有阵列添加到密钥result将覆盖所有条目并仅输出最后一个条目。

谢谢

+0

你想为JSON完全相同的对象结构,你已经列出的XML? –

+0

是的..基本上我只是想在该数组中构造输出,并根据需要(xml或json)我想简单地将数组编码为xml或json – Kartik

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

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

〔实施例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> 

我不认为你需要使用这个库。

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

您好,我知道我可以这样做,但该库也支持嵌套数组..但是,这并没有帮助我,因为当我编码与JSON它只是显示条目,但没有标签结果标签的条目,它只显示条目.. - – Kartik

假设您的外部数组被调用$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。

的foreach($数据,$关键=> $值){

    //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); 
        } 
      }