2014-02-10 18 views
-1

我有一個多維數組是這樣的:轉換多維數組到XML對象在PHP

$array = array(

    "hello" => "hola", 

    "another_array" => array(
     "key" => "best key ever", 
     "another" => "yes, another key", 
    ), 

    "coolarray" => array(
     "bool" => true, 
     "string" => "this is a string!", 
    ), 
); 

我想這樣的一個類:

class MyClass { 

    public $array; 

    public function __construct($array) { 
     // something 
     $this->array_to_xml($array); 
    } 

    public function array_to_xml($array) { 
     // convert array to xml 
    } 

然後,我希望能夠做到像這樣的事情:

$string = $this->array->coolarray->string; 

我該怎麼做?

+0

@Martijn我已經在嘗試的答案3,我仍然不知道如何使用它作爲這樣一個對象:'$ string = $ this-> array-> coolarray-> string;' – James

回答

1

這個被問了很多

不知道爲什麼你提到XML,聽起來像是你只想一個對象。

看到這個答案,例如: https://stackoverflow.com/a/11854285/543455

$obj = json_decode(json_encode($array));

+0

以前從未使用過JSON。我會放棄這一點。我提到XML是因爲我正在製作自己版本的使用XML轉換的開源項目。我試圖修改它,然後破壞了它。 – James

+0

更新:這是說我試圖獲得非對象的屬性。 – James

+0

好的,這是非常糟糕的習慣。看看這個答案:http://stackoverflow.com/a/4790485/801496這是更好的方法。 – Hast