2011-07-22 80 views
2

我想從我的數據庫中獲取一些數據,然後嘗試在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

回答

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> 

〔實施例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> 
2

我不認爲你需要使用這個庫。

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

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。

0

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