Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中将多维数组转换为XML对象_Php_Xml_Arrays_Object_Multidimensional Array - Fatal编程技术网

在PHP中将多维数组转换为XML对象

在PHP中将多维数组转换为XML对象,php,xml,arrays,object,multidimensional-array,Php,Xml,Arrays,Object,Multidimensional Array,我有一个多维数组,如下所示: $array = array( "hello" => "hola", "another_array" => array( "key" => "best key ever", "another" => "yes, another key", ), "coolarray" => array( "bool" => true, "str

我有一个多维数组,如下所示:

$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;
我想要这样的课程:

$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;
然后我想做这样的事情:

$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;
我该怎么做呢?

这个问题被问了很多

不知道为什么要提到XML,听起来你只是想要一个对象

例如,请参见此答案:


$obj=json_解码(json_编码($array))

@Martijn我已经尝试了其中的3个答案,但我仍然不知道如何将它用作这样的对象:
$string=$this->array->coolarray->string以前从未使用过JSON。我要试一试。我提到XML是因为我正在制作一个使用XML转换的开源项目的自己版本。我试图修改它,但我弄坏了它。更新:这是说我试图获取非对象的属性。好吧,这是一个非常糟糕的做法。看看这个答案:这是一个更好的方法。我保留对不良行为电话的判断。两个内置函数调用与递归用户land函数相比,我真的不确定什么是最好的方法,我怀疑这在很大程度上取决于要转换的数组